Javascript 父指令上的角度表';带require的s控制器
我有一个parent和child指令,每个指令都有一个控制器,这两个指令在child指令中都是必需的。我想在子指令的link函数中观察对父指令控制器上属性的更改。但是,在初始化时启动watch函数,但在通过父指令作用域中的按钮或父指令的link函数更改属性时不会启动watch函数 请有人解释一下为什么会这样,我应该如何解决 上级指令Javascript 父指令上的角度表';带require的s控制器,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个parent和child指令,每个指令都有一个控制器,这两个指令在child指令中都是必需的。我想在子指令的link函数中观察对父指令控制器上属性的更改。但是,在初始化时启动watch函数,但在通过父指令作用域中的按钮或父指令的link函数更改属性时不会启动watch函数 请有人解释一下为什么会这样,我应该如何解决 上级指令 myApp.directive('parentDirective', function ($timeout) { return { rest
myApp.directive('parentDirective', function ($timeout) {
return {
restrict: 'E',
scope: true,
controllerAs: 'parentCtrl',
controller: function () {
var vm = this;
vm.someProperty = true;
vm.toggle = function () {
vm.someProperty = !vm.someProperty;
}
},
link: function (scope, element, attrs, controller) {
$timeout(function () {
controller.toggle();
}, 1000);
}
} });
myApp.directive('childDirective', function () {
return {
restrict: 'E',
scope: true,
require: ['childDirective', '^parentDirective'],
controllerAs: 'childCtrl',
controller: function () {
var vm = this;
vm.someProperty = '';
},
link: function (scope, element, attrs, controllers) {
var controller = controllers[0];
var parentController = controllers[1];
scope.$watch('parentController.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
}
}
});
子指令
myApp.directive('parentDirective', function ($timeout) {
return {
restrict: 'E',
scope: true,
controllerAs: 'parentCtrl',
controller: function () {
var vm = this;
vm.someProperty = true;
vm.toggle = function () {
vm.someProperty = !vm.someProperty;
}
},
link: function (scope, element, attrs, controller) {
$timeout(function () {
controller.toggle();
}, 1000);
}
} });
myApp.directive('childDirective', function () {
return {
restrict: 'E',
scope: true,
require: ['childDirective', '^parentDirective'],
controllerAs: 'childCtrl',
controller: function () {
var vm = this;
vm.someProperty = '';
},
link: function (scope, element, attrs, controllers) {
var controller = controllers[0];
var parentController = controllers[1];
scope.$watch('parentController.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
}
}
});
查看
<parent-directive>
<button ng-click="parentCtrl.toggle()">Toggle message</button>
<child-directive>
<p>{{childCtrl.someProperty}}</p>
</child-directive>
</parent-directive>
切换消息
{{childCtrl.someProperty}
.在您正在监视的范围上,父控制器是“parentCtrl”而不是“parentController”,因此您实际上没有监视您想要监视的属性。以下方面应起作用:
scope.$watch('parentCtrl.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
在您正在监视的范围中,父控制器是“parentCtrl”而不是“parentController”,因此您实际上没有监视您想要的属性。以下方面应起作用:
scope.$watch('parentCtrl.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
在您正在监视的范围中,父控制器是“parentCtrl”而不是“parentController”,因此您实际上没有监视您想要的属性。以下方面应起作用:
scope.$watch('parentCtrl.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
在您正在监视的范围中,父控制器是“parentCtrl”而不是“parentController”,因此您实际上没有监视您想要的属性。以下方面应起作用:
scope.$watch('parentCtrl.someProperty', function () {
controller.someProperty = parentController.someProperty
? 'Hello world!' : 'Goodbye cruel world';
});
谢谢你,伙计。就是看不见!谢谢你,伙计。就是看不见!谢谢你,伙计。就是看不见!谢谢你,伙计。就是看不见!