【发布时间】:2016-09-13 22:49:59
【问题描述】:
我是 Angular 新手,我需要在 Angular js 中调用另一个控制器中的函数。这怎么可能?请帮助我提前谢谢
- HTML 文件
<a> <i ng-click="open('sm')"></i> <span>test</span> </a> <div ng-controller="MapModuleController"> <scrip enter code here`t type="text/ng-template" id="myModalContent.html"> <div class="modal-body"> {{ items }} // not display anything </div> <div class="modal-footer"> <button class="btn btn-primary" type="button" ng-click="ok()">{{ 'generic_ok' | translate}}</button> </div> </script> </div>
2.控制器
app.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance,items) {
$scope.items = items;
$scope.ok = function () {
$uibModalInstance.dismiss('cancel');
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
app.controller('MapModuleController', ['$scope','$uibModal',
'$rootScope',function ($scope, $uibModal, $rootScope, ) {
$scope.testFunction = function(){
// want to call this function on press of okay button
};
$scope.msg = 'done';
$scope.open = function () {
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
return $scope.msg;
}
}
});
});
我是新来的所以请禁止我格式化。
【问题讨论】:
-
从角度来说,它们是一种在指令和控制器之间绑定函数的方法,但我不确定控制器和控制器..但是为什么需要从另一个控制器调用控制器函数.. .它背后的主要目的是什么???