【问题标题】:Unable to call function as $rootScope is undefined无法调用函数,因为 $rootScope 未定义
【发布时间】:2017-02-14 15:45:18
【问题描述】:

当在 SelectNotificationCtlr 中调用 getNotification 时,我试图调用控制器“通知”内的函数。但是,在运行此程序时,我收到一条错误消息,指出在 console.log("log"); 下面的行中 $rootScope 未定义。我尝试在 getNotification 函数中将 $rootScope 作为参数传递,但仍然收到相同的错误。

请在下面查看我的代码,任何建议或帮助将不胜感激。

app.js

selectNotification.controller('selectNotificationCtlr', ['$scope', '$rootScope', 'notificationsService',
    function($scope, $http, notificationsService, notificationService, $rootScope) {
        $scope.getNotification = function() {
            var id = $scope.selectedNotification;
            notificationData = notificationsService.getNotifications();
            console.log(notificationData);

            console.log("log");
            $rootScope.$emit("call", {});
        }
    }
]);


selectNotification.controller('Notification', ['$scope', '$rootScope',
    function($scope, $rootScope) {
        $rootScope.$on("call", function() {
            $scope.parent(notificationService);
        });

        $scope.parent = function() {
            console.log("log");
        }
    }
]);

亲切的问候

CB

【问题讨论】:

    标签: javascript angularjs function web rootscope


    【解决方案1】:

    你的控制器应该有正确顺序的依赖,

    selectNotification.controller('selectNotificationCtlr', ['$scope', '$rootScope', 'notificationsService',
        function($scope, $rootScope, notificationsService) {
    

    【讨论】:

    • 是的,完美运行。这么简单的事情,却很容易被忽视。再次感谢您,现在早上好 100%。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2017-06-08
    • 2021-07-05
    相关资源
    最近更新 更多