我已经编辑了您的代码pen源代码,我相信有更简单的方法来做到这一点,但我刚刚尝试了这个并且它可以工作,它应该让您走上正确的道路:
<ul ng-app="app" ng-controller="ctrl">
<dir model="data.children" child="child" ng-repeat="child in data.children"></dir>
</ul>
var app = angular.module('app', []);
app.controller('ctrl', function($scope){
$scope.data = {};
$scope.data.children = [
{name: 'Ben'},
{name: 'Heffler'},
{name: 'Schubert'}
];
$scope.$watchCollection('data.children', function(){
console.log("children shallow watch", $scope);
});
$scope.$watch('data.children', function(){
console.log("children deep watch",$scope);
}, true);
});
app.directive('dir', function(){
return {
restrict: 'E',
scope: {
model: '=',
child:'='
},
replace: true,
template: '<div>{{child.name}} <button ng-click="remove()">Remove</button></div>',
link: function(scope, element, attrs){
scope.remove = function(){
delete(scope.model[0]);
console.log("children inner scope", scope)
}
}
};
});
我不确定你为什么想要删除这些属性,但我相信你有你的原因,只是想向你展示它是可以实现的。
编辑
这里是经过编辑的 CodePen(请查看控制台日志以查看作用域中已删除的项目)。http://cdpn.io/Ghmvk