gpt4 book ai didi

angularjs - 为什么要将 Controller 放在 AngularJS 的指令中?

转载 作者:行者123 更新时间:2023-12-04 19:10:05 25 4
gpt4 key购买 nike

我最近看到了这个例子。这是我第一次在指令中看到 Controller 。这是正常的事情吗。我认为您应该将这两个保留在不同的区域以进​​行可测试性:

myModule.directive('myComponent', function(mySharedService) {
return {
restrict: 'E',
controller: function($scope, $attrs, mySharedService) {
$scope.$on('handleBroadcast', function() {
$scope.message = 'Directive: ' + mySharedService.message;
});
},
replace: true,
template: '<input>'
};
})

最佳答案

通常,您会在指令中使用 Controller ,以便能够在一个元素上的指令之间共享它们。它允许指令轻松地在它们之间进行通信。

请参阅此处以了解其工作原理:http://egghead.io/video/angularjs-directive-to-directive-communication/

关于angularjs - 为什么要将 Controller 放在 AngularJS 的指令中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com