嵌套模板上的Angular js范围

时间:2015-08-27 11:49:12

标签: javascript angularjs

我有以下指令,我一直用作

#define int my_int

<ui-submit-button btn-state='btnSendProcessing' /> 是控制器范围内的一个值,它会在触发http请求时打开和关闭。

btnSendProcessing有效,但未显示处理图标。

ng-disabled

这可能是什么问题?

小提琴按预期工作http://jsfiddle.net/to5y9kud/1/

1 个答案:

答案 0 :(得分:1)

在范围继承中,基本上在任何js原型继承中,如果使用原语,它会将原始值复制到继承的对象。

如果您希望更改同时影响父范围和继承范围,则应使用Array或Object,这是一个示例,它解释了继承范围的正常行为:

$parentScope.a = 1;
$parentScope.b = {a:1};
$childScope = $parentScope.$new() //inherit from $parentScope

console.log($childScope.a === 1, $childScope.b.a === 1) //true, true
$childScope.a = 2;
$childScope.b.a = 2;
console.log($parentScope.a === 2, $parentScope.b.a === 2) //false, true

所以在你的情况下btnSendProcessing应该是一个对象而不是一个原始对象。这是一个工作小提琴:http://jsfiddle.net/avp9ye1g/1/

阅读这个很好的答案,以便更好地理解:Understanding Javascript immutable variable;