【问题标题】:calling function of one controller from another从另一个控制器调用一个控制器的功能
【发布时间】:2016-09-13 22:49:59
【问题描述】:

我是 Angular 新手,我需要在 Angular js 中调用另一个控制器中的函数。这怎么可能?请帮助我提前谢谢

  1. 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;
                            }
                        }

                    });
    });

我是新来的所以请禁止我格式化。

【问题讨论】:

  • 从角度来说,它们是一种在指令和控制器之间绑定函数的方法,但我不确定控制器和控制器..但是为什么需要从另一个控制器调用控制器函数.. .它背后的主要目的是什么???

标签: angularjs angularjs-scope


【解决方案1】:

您可以将一个控制器注入另一个控制器,如下所示:

'use strict';

reachApp.controller('dashboardController', ['$scope','$controller', function ($scope, $controller) {
                var feedbackModel = $scope.$new(); 
                 $controller('feedbackController',{$scope : feedbackModel });
                 feedbackModel.getLatestMeetingRating(); //And call the method on the newScope.
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2011-08-30
    • 2016-10-08
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多