最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: input not binding to expected scope when inside a view - Stack Overflow

programmeradmin5浏览0评论

I'm having an issue with binding an input from inside a view. I thought it would bind to the controller scope, but it seems to be binding to a child scope, so it's not updating above.

Other items will bind like I expect if they're inside an ng-repeat (I'm not sure why).

Here's an example:
/

What's the best way to bind the input to the correct scope if it's not inside an ng-repeat?

I'm having an issue with binding an input from inside a view. I thought it would bind to the controller scope, but it seems to be binding to a child scope, so it's not updating above.

Other items will bind like I expect if they're inside an ng-repeat (I'm not sure why).

Here's an example:
http://jsfiddle/hMpsB/1/

What's the best way to bind the input to the correct scope if it's not inside an ng-repeat?

Share Improve this question asked Oct 8, 2012 at 21:29 valanvalan 1351 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

In your example you will have better luck binding your $scope.test to an object instead of a primitive type like this:

$scope.test = { val: "test value" };

You can see this fiddle for a working example.

The child scope that gets created in the ngView will copy your value and since your original $scope.test is a primitive string it has no link to the parent value so your input will be modifying the child scope copy. When binding to an object your child scope has a copy of the object reference but will ultimately modify the same instance of the object.

You can take a look at this question for more information on creating a service to persist data across multiple controllers (which is a little similar to your question).

You can also look into using $parent as described in this answer though as Mark mentions it's undocumented and might get messy if another child scope ever gets introduced somewhere.

发布评论

评论列表(0)

  1. 暂无评论