【发布时间】:2015-09-29 20:25:10
【问题描述】:
解释如下:
我的当前控制器创建了一个 $scope.plan.steps 数组,用于存储每个步骤:
.controller('PlanCtrl', function ($scope, $http) {
$scope.plan = {
steps: [{}]
};
$scope.addStep = function () {
$scope.tutorial.steps.push({});
}
}
然后我有以下指令,它有一个独立的范围,并且与 $scope.plan.steps 数组的索引相关联:
.directive('planStep', function () {
return {
template: '<input type="text" ng-model="step.name" />{{step}}',
restrict: 'E',
scope: {
index: '=index'
},
transclude: true,
controller: function($scope, $element, $transclude) {
$scope.removeStep = function() {
$scope.$emit('removeStep', $scope.index);
$element.remove();
$scope.$destroy();
}
}
};
});
这两个在控制器范围内通信、创建和删除对象,但是,我怎样才能允许指令实时更新控制器的范围数组?
我尝试对指令的隔离范围更改执行 $watch,$emit 对控制器的更改,并指定 $index...但没有运气。
我创建了一个 plunker 来重现我目前拥有的东西:
Link
到目前为止,我可以在数组内创建和删除对象,但我无法获取单个对象来根据 $index 更新控制器的对象。
如果解释不清楚,一定要告诉我,我会详细说明。
谢谢
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat