Javascript AngularJS:来自控制器的访问指令
背景 我有一个需要控制器访问的顶级指令。请考虑这个问题。 指令Javascript AngularJS:来自控制器的访问指令,javascript,.net,angularjs,asp.net-mvc,Javascript,.net,Angularjs,Asp.net Mvc,背景 我有一个需要控制器访问的顶级指令。请考虑这个问题。 指令 app.directive('topDirective', ['$compile', function($scope){ return { scope: {}, restrict: 'E', template: '<h3>Top Directive</h3><p><button ng-click="CallMe()">Click Me</button&
app.directive('topDirective', ['$compile', function($scope){
return {
scope: {},
restrict: 'E',
template: '<h3>Top Directive</h3><p><button ng-click="CallMe()">Click Me</button></p>',
controller: function($scope) {
var self = {};
$scope.CallMe = function(){
alert('Call Me');
};
},
link: function($scope, $elem, $attrs, $ctrl) {
}
};
}]);
app.directive('topDirective',['$compile',函数($scope){
返回{
作用域:{},
限制:'E',
模板:“Top指令单击我”,
控制器:功能($scope){
var self={};
$scope.CallMe=函数(){
警惕(“打电话给我”);
};
},
链接:函数($scope、$elem、$attrs、$ctrl){
}
};
}]);
需要访问的控制器
app.controller('subController', [
'$scope',
function($scope){
var self = {};
$scope.CallDirective = function() {
alert('>>> Replace by call to directive function CallMe (somehow) <<<')
};
}]);
app.controller('分包商'[
“$scope”,
职能($范围){
var self={};
$scope.CallDirective=函数(){
警报('>>>Replace by call to directive function CallMe(以某种方式)>>Replace by call to directive function CallMe(以某种方式)在控制器中发出事件
app.controller('subController', [
'$scope','$rootScope',
function($scope,$rootScope){
var self = {};
$scope.CallDirective = function() {
var data ='This is new data';
$rootScope.$emit('callDirective',data);
};
}]);
在指令中,你可以这样做
app.directive('topDirective', ['$compile', function($scope){
return {
scope: {},
restrict: 'E',
template: '<h3>Top Directive</h3><p><button ng-click="CallMe()">Click Me</button></p>',
controller: function($scope,$rootScope) {
var self = {};
$scope.CallMe = function(data){
alert(data);
};
$rootScope.$on('callDirective',function(type,data){
$scope.CallMe(data);
});
},
link: function($scope, $elem, $attrs, $ctrl) {
}
};
}]);
app.directive('topDirective',['$compile',函数($scope){
返回{
作用域:{},
限制:'E',
模板:“Top指令单击我”,
控制器:函数($scope,$rootScope){
var self={};
$scope.CallMe=函数(数据){
警报(数据);
};
$rootScope.$on('callDirective',函数(类型,数据){
$scope.CallMe(数据);
});
},
链接:函数($scope、$elem、$attrs、$ctrl){
}
};
}]);
在控制器中发出事件
app.controller('subController', [
'$scope','$rootScope',
function($scope,$rootScope){
var self = {};
$scope.CallDirective = function() {
var data ='This is new data';
$rootScope.$emit('callDirective',data);
};
}]);
在指令中,你可以这样做
app.directive('topDirective', ['$compile', function($scope){
return {
scope: {},
restrict: 'E',
template: '<h3>Top Directive</h3><p><button ng-click="CallMe()">Click Me</button></p>',
controller: function($scope,$rootScope) {
var self = {};
$scope.CallMe = function(data){
alert(data);
};
$rootScope.$on('callDirective',function(type,data){
$scope.CallMe(data);
});
},
link: function($scope, $elem, $attrs, $ctrl) {
}
};
}]);
app.directive('topDirective',['$compile',函数($scope){
返回{
作用域:{},
限制:'E',
模板:“Top指令单击我”,
控制器:函数($scope,$rootScope){
var self={};
$scope.CallMe=函数(数据){
警报(数据);
};
$rootScope.$on('callDirective',函数(类型,数据){
$scope.CallMe(数据);
});
},
链接:函数($scope、$elem、$attrs、$ctrl){
}
};
}]);
您可以从控制器广播并在Directive上收听。您可以从控制器广播并在Directive上收听。它工作正常,谢谢!我更新了我的。它工作正常,谢谢!我更新了我的。