【发布时间】:2018-11-01 15:06:47
【问题描述】:
第一条指令:
app.directive("myDirectiveOne", function($rootScope){
return {
templateUrl : "/custom-one-html.html",
restrict: "AE",
replace:true,
scope: {
somedata: "=",
flags: "=",
functionone: "&"
}
,controller: function($rootScope,$scope, $element) {
$scope.firstFunction = function(){
console.log("First function is getting called")
}
$scope.$on('firstBroadcast',function(event, data){
$rootScope.$broadcast('secondBroadcast', data)
});
}
第二条指令:
app.directive("myDirectiveTwo", function($rootScope){
return {
templateUrl : "/custom-two-html.html",
restrict: "AE",
replace:true,
scope: {
data: "=",
functiontwo: "&"
}
,controller: function($rootScope,$scope, $element) {
$scope.secondFunction = function(){
console.log("Second function is getting called")
$rootScope.$broadcast('firstBroadcast', {})
}
$scope.$on('secondBroadcast',function(event, data){
$scope.callSomeFunctionWithData(data);
});
$scope.secondFunction();
$scope.editFunction = function(x){
console.log("This is the edit function", x);
}
父控制器:
$scope.parentFuntion = function(){
console.log("No trouble in calling this function")
}
所以,问题是当我尝试从myDirectiveTwo html 模板调用函数时,处于活动状态的控制器是父控制器,而不是孤立的控制器。
可能与我使用的广播有关?
HTML代码:
<div ng-repeat="x in data">
<h5>{{x.name}}</h5>
<button ng-click="editFunction(x)">Edit</button>
</div>
奇怪的是我得到了数据值并且 ng-repeat 在加载时工作正常。但是,当我单击按钮时,它什么也没做。如果我在父控制器中添加相同的功能,它会被调用.. :( 如何使隔离范围控制器再次处于活动状态..?
【问题讨论】:
-
没有什么明显的错误,您能否也粘贴一些 HTML 或复制问题的 JSFiddle?谢谢
-
@Protozoid 添加了 html 代码。
标签: angularjs angularjs-directive angularjs-scope angular-controller angular-broadcast