Javascript 如何从不同的控制器AngularJS调用控制器中的函数
我正在研究如何强制一个控制器从另一个控制器刷新 为此,我使用了一个简单的测试函数:Javascript 如何从不同的控制器AngularJS调用控制器中的函数,javascript,angularjs,Javascript,Angularjs,我正在研究如何强制一个控制器从另一个控制器刷新 为此,我使用了一个简单的测试函数: function test() { alert('test'); } 我认为使用事件是解决此问题的最佳解决方案 我使用了以下代码: 第一控制器 .controller('firstcontroller', function($rootScope,$scope, $http,$timeout) { $scope.$emit('testevent'); }) .controller('firstcontrolle
function test() { alert('test'); }
我认为使用事件是解决此问题的最佳解决方案
我使用了以下代码:
第一控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
第二控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
但这是行不通的!有什么建议吗?您可以使用$rootScope.$broadcast函数将事件广播到所有子作用域。然后在控制器中执行$scope.$on。但要确保事件的名称相同 应该是这样 第一控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
第二控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
您可以使用$rootScope.$broadcast函数将事件广播到所有子作用域。然后在控制器中执行$scope.$on。但要确保事件的名称相同 应该是这样 第一控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
第二控制器
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$emit('testevent'); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('refreshr', function (event) {
alert('test');
}); })
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
});
})
您的
事件名称
不同,也在rootScope
级别上广播
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
})
})
您的
事件名称
不同,也在rootScope
级别上广播
.controller('firstcontroller', function($rootScope,$scope, $http,$timeout) {
$rootScope.$broadcast('testevent');
})
.controller('secondcontroller', function($rootScope,$scope, $http,$timeout) {
$scope.$on('testevent', function (event) {
alert('test');
})
})
谢谢你的回答!你能根据我的代码举个例子吗!谢谢你的回答!你能根据我的代码举个例子吗!在提供的代码中,两个事件没有相同的名称('testevent','Refresh',?,两个控制器都有相同的名称('firstcontroller')?请参阅对类似线程的此回答:在提供的代码中,两个事件没有相同的名称('testevent','Refresh',?,两个控制器都有相同的名称('firstcontroller')?请参阅对类似线程的此回答: