数据绑定如何在AngularJS框架中工作?
我没有在他们的网站上找到技术细节.当数据从视图传播到模型时,它或多或少清楚它是如何工作的.但是AngularJS如何在没有setter和getter的情况下跟踪模型属性的变化?
我发现有一些JavaScript观察者可以做这项工作.但Internet Explorer 6和Internet Explorer 7不支持它们.那么AngularJS如何知道我改变了例如以下内容并在视图上反映了这一变化?
myobject.myproperty="new value";
Run Code Online (Sandbox Code Playgroud) 我有一个带有隔离范围的指令,其值为双向绑定到父范围.我正在调用一个方法来更改父作用域中的值,但是我的指令中没有应用更改.(不会触发双向绑定).这个问题非常相似:
AngularJS:父范围未在指令(具有隔离范围)双向绑定中更新
但我没有更改指令中的值,只是在父作用域中更改它.我读了解决方案,在第五点说:
The watch() created by the isolated scope checks whether it's value for the bi-directional binding is in sync with the parent's value. If it isn't the parent's value is copied to the isolated scope.
Run Code Online (Sandbox Code Playgroud)
这意味着当我的父值更改为2时,将触发监视.它检查父值和指令值是否相同 - 如果不相同,则复制到指令值.好的,但我的指示值仍然是1 ...我错过了什么?
HTML:
<div data-ng-app="testApp">
<div data-ng-controller="testCtrl">
<strong>{{myValue}}</strong>
<span data-test-directive data-parent-item="myValue"
data-parent-update="update()"></span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
var testApp = angular.module('testApp', []);
testApp.directive('testDirective', function ($timeout) {
return {
scope: {
key: '=parentItem',
parentUpdate: '&'
},
replace: true,
template:
'<button data-ng-click="lock()">Lock</button>' +
'</div>',
controller: …Run Code Online (Sandbox Code Playgroud)