我是使用 Angular js 的新手,我已经声明了很多 Controller ,现在我想将一个 Controller 的用户功能转换为另一个 Controller 。这是我的示例代码。
app.controller('Controller1',function($scope,$http,$compile){
$scope.test1=function($scope)
{
alert("test1");
}
});
app.controller('Controller2',function($scope,$http,$compile){
$scope.test2=function($scope)
{
alert("test1");
}
});
app.controller('Controller3',function($scope,$http,$compile){
///
});
现在我想在 controller3 中调用 test2 函数。 任何人都可以帮助.. 感谢 Avance...:)
最佳答案
您不能在 Controller 中调用 Controller 中的方法。您需要提取该方法,创建一个服务并调用它。这也将使代码彼此解耦并使其更易于测试
(function() {
angular.module('app', [])
.service('svc', function() {
var svc = {};
svc.method = function() {
alert(1);
}
return svc;
})
.controller('ctrl', [
'$scope', 'svc', function($scope, svc) {
svc.method();
}
]);
})();
关于javascript - 从另一个 Controller Angular Js 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25562905/