首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更新AngularJS中的父作用域变量

更新AngularJS中的父作用域变量
EN

Stack Overflow用户
提问于 2013-06-05 06:08:36
回答 5查看 82.2K关注 0票数 113

我有两个控制器,一个包装在另一个中。现在我知道子作用域继承了父作用域的属性,但是有没有办法更新父作用域变量呢?到目前为止,我还没有发现任何明显的解决方案。

在我的情况下,我在一个表单中有一个日历控制器。我想从父作用域(即表单)更新开始和结束日期,以便表单在提交时具有开始和结束日期。

EN

Stack Overflow用户

回答已采纳

发布于 2013-06-05 07:27:57

您需要在父作用域中使用对象(而不是基元),然后才能直接从子作用域更新它

父级:

代码语言:javascript
运行
复制
app.controller('ctrlParent',function($scope){
    $scope.parentprimitive = "someprimitive";
    $scope.parentobj = {};
    $scope.parentobj.parentproperty = "someproperty";
});

子代:

代码语言:javascript
运行
复制
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?

票数 194
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16928341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档