我有两个控制器,一个包装在另一个中。现在我知道子作用域继承了父作用域的属性,但是有没有办法更新父作用域变量呢?到目前为止,我还没有发现任何明显的解决方案。
在我的情况下,我在一个表单中有一个日历控制器。我想从父作用域(即表单)更新开始和结束日期,以便表单在提交时具有开始和结束日期。
发布于 2013-06-05 07:27:57
您需要在父作用域中使用对象(而不是基元),然后才能直接从子作用域更新它
父级:
app.controller('ctrlParent',function($scope){
$scope.parentprimitive = "someprimitive";
$scope.parentobj = {};
$scope.parentobj.parentproperty = "someproperty";
});子代:
app.controller('ctrlChild',function($scope){
$scope.parentprimitive = "this will NOT modify the parent"; //new child scope variable
$scope.parentobj.parentproperty = "this WILL modify the parent";
});工作演示:http://jsfiddle.net/sh0ber/xxNxj/
请参阅What are the nuances of scope prototypal / prototypical inheritance in AngularJS?
https://stackoverflow.com/questions/16928341
复制相似问题