Javascript 从另一个控制器调用函数
如何从另一个控制器调用函数loadTableJavascript 从另一个控制器调用函数,javascript,angularjs,Javascript,Angularjs,如何从另一个控制器调用函数loadTable app.controller('tableCtrl', ['$scope', function (scope) { scope.loadTable = function() { //some code }; }); app.controller('secondCtrl', ['$scope', function (scope) { scope.buttonClick =
app.controller('tableCtrl', ['$scope', function (scope) {
scope.loadTable = function() {
//some code
};
});
app.controller('secondCtrl', ['$scope', function (scope) {
scope.buttonClick = function() {
//call loadTable from tableCtrl
};
});
尝试$emit-$on
(发布-订阅)机制
app.controller('tableCtrl', ['$scope', function (scope) {
scope.loadTable = function() {
//some code
};
$rootScope.$on('load-table', function (event, arg1, arg2) {
// now trigger the method.
scope.loadTable();
});
});
app.controller('secondCtrl', ['$scope', function (scope) {
scope.buttonClick = function() {
//call loadTable from tableCtrl
//emit the event
$rootScope.$emit('load-table', 'arg1', 'arg2');
};
});
尝试$emit-$on
(发布-订阅)机制
app.controller('tableCtrl', ['$scope', function (scope) {
scope.loadTable = function() {
//some code
};
$rootScope.$on('load-table', function (event, arg1, arg2) {
// now trigger the method.
scope.loadTable();
});
});
app.controller('secondCtrl', ['$scope', function (scope) {
scope.buttonClick = function() {
//call loadTable from tableCtrl
//emit the event
$rootScope.$emit('load-table', 'arg1', 'arg2');
};
});
您可能应该使用以下服务:
app.factory('tableService', [function () {
return {
loadTable : function() {
//some code
}
};
});
app.controller('tableCtrl', ['$scope', 'tableService', function (scope, tableService) {
});
app.controller('secondCtrl', ['$scope', 'tableService', function (scope, tableService) {
scope.buttonClick = function() {
tableService.loadTable();
};
});
然后,您的tableCtrl需要使用服务上的另一个功能保存任何数据。您可能应该使用以下服务:
app.factory('tableService', [function () {
return {
loadTable : function() {
//some code
}
};
});
app.controller('tableCtrl', ['$scope', 'tableService', function (scope, tableService) {
});
app.controller('secondCtrl', ['$scope', 'tableService', function (scope, tableService) {
scope.buttonClick = function() {
tableService.loadTable();
};
});
然后,您的tableCtrl需要使用服务上的另一个函数保存任何数据。重复使用$rootScope进行尝试,更多信息,请尝试使用$rootScope进行教程。重复使用$rootScope进行尝试,更多信息,请尝试使用$rootScope进行教程。是的,我正在尝试记住语法。这样就行了。是的,我试着记住语法。这就行了。