我想要完成的是绑定两个指令抛出一个共享服务,以便当其中一个发生更改时,更改将立即传播到另一个。
我的指令如下所示:
app.directive("input1", function (sharedService) {
return {
restict: 'A',
scope: 'isolate',
template: '<input type="text" ng-model="sharedText" class="input-medium" />{{sharedText}}',
link: function (scope, elem, attrs) {
scope.sharedText = sharedService.sharedText; // Service never gets updated if the scope it's modified :(
}
}
});
app.directive("input2", function (sharedService) {
return {
restict: 'A',
scope: 'isolate',
template: '<input type="text" ng-model="sharedText" class="input-medium" />{{sharedText}}',
link: function (scope, elem, attrs) {
scope.sharedText = sharedService.sharedText;
}
}
});
在这里,您可以看到我到目前为止所拥有的小提琴:http://jsfiddle.net/Hubrus/kxGG6/1/
谢谢!