为什么这个$ scope。$ on不起作用?

时间:2016-10-19 06:35:43

标签: javascript angularjs

使用angular,我写了一个测试来获取当前的用户信息。这是我的代码:

    var app = angular.module('gzmu', ["ngRoute",'chart.js']);
    app.run(function ($rootScope, $http) {

        $http({
            method: 'GET',
            url: 'datacon/user_info.php',

        }).success(function (response) {
                  //response is ready
            $rootScope.$broadcast("userinfo", response[0]);



        })
    });
    app.controller('data', function ($scope, $http, $rootScope) {
        $scope.username='';
        $scope.$on("userinfo",
            function (event, msg) {
//this function not work every time,when i reload the page,no alert is pop out

                if(msg){
                    $scope.username = msg.user;
                    console.log($scope.username)
                    alert($scope.usernamea)
                }
                else{
                    alert(msg);
                }

            });
    });

如果我想询问是否要在$scope.username中设置$on,我该怎么办?

0 个答案:

没有答案