AngularJS:广播&范围。$ watch(“on”)无效

时间:2014-02-25 20:34:50

标签: event-handling singleton broadcast dispatch

我是AngularJS的新手,我想知道为什么我的派遣事件没有解雇。我遵循了一些教程,是因为我在2个控制器中调用了我的服务工厂吗?

JavaScript的:

var app = angular.module('myApp', []);

app.controller('mainController', ['$scope', 'errorFactory', function($scope, errorFactory) {

    $scope.person = "Harvey Specter";

    errorFactory.showError("", false, "os_support");
}]);

// Error handling controller
app.controller('errorController', ['$scope', 'errorFactory', function($scope,     errorFactory) {
    $scope.error_message = "";

    // Whenever show-error event occured
    $scope.$on("show-error", function(e, errors) {
        $scope.error_message = errors;
    });
}]);

/*
 * Error handling factory
 */ 
app.factory('errorFactory', ['$rootScope', function($rootScope) {
    var errorHandling = {
        showError: function(message, valid, type) {
            aErrors = { message: message, valid: valid, type: type };
            $rootScope.$broadcast('show-error', aErrors);
        }
    };
    return errorHandling;
}]);

HTML文件:

    <div data-ng-controller="mainController">
        <div id="wrapper">
            <p>{{ person }}</p>
        </div>
    </div>

    <div data-ng-controller="errorController">
        <div class="error-handling" data-ng-if="!error_message.valid">
            <div class="inner-center">
                <h2 class="title">OS Not working.</h2>
            </div>
        </div>
    </div>

谢谢

0 个答案:

没有答案