我有一个 Angular 指令 esResize
,在隔离作用域上使用双向绑定时遇到了问题。
window.estimation.directive('esResize', function() {
return {
restrict:'C',
scope: {
pointGrid: '='
},
template: "<h2>{{ pointGrid }}</h2><ul><li ng-repeat='card in pointGrid track by $id(card)'>{{ card }}</li></ul>",
controller: function($scope) {
$scope.shiftTicket = function() {
$scope.pointGrid.push("New Ticket");
};
},
link: function(scope, element, attrs) {
var height;
element.resizable({
grid: [ 10, 20 ],
handles: "n, s",
start: function(event, ui) {
//initialize method
},
resize: function(event, ui) {
scope.shiftTicket();
}
});
}
};
});
当触发
resize
时,当我将pointGrid
输出到控制台时,其中添加了“New Ticket”。但是模板没有更新。我是否误解了绑定与视图的关联方式,还是其他原因在起作用?编辑:视图中的
pointGrid
只是一个数组。
pointGrid: '='
改为pointGrid: '&'
。我刚刚遇到了同样的问题,通过这种方式解决了。尝试调用scope.$apply
了吗? - WebWanderer