更新父作用域变量 [英] Update parent scope variable

查看:94
本文介绍了更新父作用域变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器之一中的另一个包裹。现在我知道孩子的范围从父范围继承属性,但有没有办法更新父范围的变量?到目前为止,我还没有碰到过任何明显的解决方案。

I have two controllers one wrapped within another. Now I know the child scope inherits properties from the parent scope but is there a way to update the parent scope variable? So far I have not come across any obvious solutions.

在我的情况我有一个表格内的日历控制器。我想更新从父范围开始和结束日期(即形式),以便提交表单时有开始和结束日期。

In my situation I have a calendar controller within a form. I would like to update the start and end dates from the parent scope (which is the form) so that the form has the start and end dates when submitted.

推荐答案

您需要在父范围使用对象(而不是原始的),然后你就可以直接从子作用域更新

You need to use an object (not a primitive) in the parent scope and then you will be able to update it directly from the child scope

父:​​

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范围是原型/原型继承的细微差别AngularJS?

这篇关于更新父作用域变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆