不知道如何在angularjs中使用$ controller

时间:2016-08-03 08:01:53

标签: javascript angularjs

我需要在我的应用程序中显示信息消息。当我在任何页面中执行任何重要操作时,消息将显示为信息消息。

这是显示消息的页面(主页)

 <body ng-controller="menubarController as ctrl">
        <div id="parent_vertical">
            <div>
                <div  style="float: left;"><img src="<c:url value="images/others/logo-meesters-300x50.png" />"  width="600" height="70" align="top"></div>
                <div ng-show="ctrl.showConfirmationMessage" ><textarea class ="messagesGrid" rows="3" cols="50"> {{ctrl.msg}}   </textarea> </div>
            </div>
    </body>

这是messageController

'use strict';

angular.module('mb').controller('menubarController',
            menubarController);

function menubarController($scope, $mdDialog) {
    var ctrl = this;
    ctrl.showConfirmationMessage = false;



    //---------------------------- show sample -------------------------------------------------------------------          
    this.sampleAction = function(name, ev) {
        ctrl.showConfirmationMessage = !ctrl.showConfirmationMessage;
        ctrl.msg = 'Hi ' +name;
        $mdDialog.show($mdDialog.alert().title(name).textContent(
                    'Start learning "' + name + '!' + ctrl.showConfirmationMessage).ok('OK').targetEvent(ev));
    };

    this.showMessage = function (text){
        ctrl.showConfirmationMessage = true;
        ctrl.msg = text;
    }
}

如果我从(主页)内部调用函数showMessage或sampleAction一切正常工作

现在我想从另一个控制器调用此函数 首先我注入$ controller

.controller(
                'usersListController',
                [
                        'userService',
                        '$uibModal',
                        '$mdDialog',
                        '$scope',
                        '$rootScope',
                        '$timeout',
                        '$controller',
                        function(service, $uibModal, $mdDialog, $scope, $rootScope, $timeout, $controller) {

这是控制器代码

this.editedUser = function (user){
                                ctrl.isSaving = true;

                                 var menubarController = $controller('menubarController');
                                    service
                                            .EditUser(
                                                    user)
                                            .then(
                                                    function handleSuccess(response) {
                                                        $timeout(function(){
                                                             angular.element('#searchForm').triggerHandler('submit');
                                                            }, 1000);

                                                        menubarController.sampleAction('Hi');
                                                    });

                                    this.showEditUserPanel = 0;

                            };

我收到以下错误

  

angular.js:12520错误:[$ injector:unpr]未知提供者:   $ scopeProvider&lt; - $ scope&lt; - menubarController

0 个答案:

没有答案