ディレクティブで新しいプロパティが設定された後、モデルが完全に上書きされないという奇妙な問題が発生しています。
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 が残っているのはなぜですか?