【问题标题】:calling one controller from another controller从另一个控制器调用一个控制器
【发布时间】:2015-09-07 14:51:55
【问题描述】:

我有 2 个控制器,我正在从第一个控制器的功能中调用第二个控制器

$scope.open = function () {
    var modal = $modal.open({
        templateUrl: 'views/view1.html',
        controller: 'controller2',
        // other parameters
    });
    /* some code */
}

两个控制器都在同一个文件夹中。有类似的问题,但我想知道是否有任何方法可以在不使用服务的情况下调用第二个控制器。

编辑 --

这是我得到的错误--

错误:[ng:areq] 参数 'controller2' 不是函数,未定义 http://errors.angularjs.org/1.3.16/ng/areq?p0=controller2&p1=not%20a%20function%2C%20got%20undefined 在 REGEX_STRING_REGEXP (angular.js:63) 在 assertArg (angular.js:1590) 在 assertArgFn (angular.js:1600) 在 angular.js:8502 在 resolveSuccess (ui-bootstrap-tpls.js:2377) 在 processQueue (angular.js:13292) 在 angular.js:13308 在 Scope.$get.Scope.$eval (angular.js:14547) 在 Scope.$get.Scope.$digest (angular.js:14363) 在 Scope.$get.Scope.$apply (angular.js:14652)

【问题讨论】:

  • 那么问题出在哪里?找不到控制器controller2 还是什么?
  • 好的,根据您的错误,这可能是原因:您没有加载控制器脚本。您没有在模块中声明 controller2 ,因此无法访问。可以放控制器定义代码吗?
  • 是的,我收到了错误,我没有在 index.html 中包含文件。

标签: angularjs


【解决方案1】:

在模态打开功能中可以正常使用控制器,但控制器2必须与控制器1在同一模块中:

angular.module('myModule').controller('controller1', ['$scope', myModuleController1]); 
angular.module('myModule').controller('controller2', ['$scope', myModuleController2]);

否则,如果您的控制器属于单独的模块 myModule1、myModule2,则您必须在 app.js 文件中正确设置依赖项:

angular.module('myModule', [myModule2]);

另外,请确保两个控制器都定义为 index.html 中的文件

    <script src="../controller1file.js"></script>
<script src="../controller2file.js"></script>

完成所有这些步骤后,您将能够在 controller1 代码中通过以下方式使用模态:

function OpenModalsService($modal) {

var OpenModalsService = {};

var _openmodal = function () {

    var modalInstance = $modal.open({
        templateUrl: 'Modals.Views/view.html',
        controller: 'controller2',
        size: 50
    });

    modalInstance.result.then(function (var) {

       var = 'something';
    });
};

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多