我遇到了一個奇怪的問題,其中一個模型在一個指令中已經設置了一個新屬性後沒有被完全覆蓋。這個Angular模型爲什麼沒有被完全覆蓋?
HTML:
<div ng-app="app">
<div ng-controller="Dashboard">
<div ng-repeat="item in items" my-dir items="items"></div>
<button ng-click="replaceItems()">Replace</button>
</div>
</div>
JS:
angular.module('app', []).controller('Dashboard', function($scope) {
$scope.items = [{foo: "bar"}];
console.log($scope.items);
$scope.replaceItems = function() {
$scope.items = [{}];
console.log($scope.items);
}
}).directive('myDir', function($injector) {
return {
scope: {
items: '='
},
link: function(scope) {
scope.items.newThing = 'I am new';
}
};
});
試着點擊更換和檢查控制檯。
爲什麼newThing仍然存在,即使$ scope.items被空白數組和對象覆蓋?