0
나는 두 지시어, 이 및을 공유 의 절연 지시어는 공유 지침에 직접 결합 양방향을 통과 고립하지만 공유 지시어가 생성되면, 절연 범위를 사용하지 않는이 그 자체.각도 범위, 신비?
목적은 공유 지시어를 변경할 때 고립 지시어는 양방향 바인딩의 변화에 대응해야한다는 것입니다.
<body ng-app="app">
<div ng-controller="main as $ctrl">
<h3>Main data: {{$ctrl.data.bind}}</h3>
<isolated bind="$ctrl.data.bind"></isolated>
</div>
</body>
angular.module("app", [])
.controller("main", function() {
this.data = {
bind: 123
}
})
.directive("isolated", function() {
return {
scope: {
bind: '='
},
bindToController: true,
template: '<div><h3>Parent directive data: {{$ctrl.bind}}</h3> </div>'
+ '<input type="text" shared ng-model="$ctrl.bind" />',
controller: function() {
this.changed = function() {
console.log('Data changed: ' + this.bind);
}
},
controllerAs: '$ctrl',
link: {
pre: function($scope) {
console.log("Parent data: " + $scope.$ctrl.bind);
}
}
}
})
.directive("shared", function() {
return {
restrict: 'A',
require: {
ngModel: '^'
},
bindToController: true,
link: function($scope) {
console.log('Current data in shared: ' + $scope.$ctrl.bind)
},
controller: function() {
this.$postLink = function() {
this.ngModel.$modelValue = 321;
}
},
controllerAs: '$ctrl'
}
});
여기에 내가 Plunker
공유 지시어에 문제가 있습니다. 'controllerAs : '$ ctrl''이것을'vm'으로 변경하면 텍스트 상자에 값을 적재하십시오. 그러나 postlink 함수는 값을 변경하지 않습니다. –