我遇到了一个奇怪的问题,即在指令中设置了新属性后模型没有完全覆盖。
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';
}
};
});
尝试单击替换并检查控制台。
即使 $scope.items 被空白数组和对象覆盖,为什么 newThing 仍然存在?