var app = angular.module('gzmu', ["ngRoute",'chart.js']);app.run(function ($rootScope, $http) { $http({ method: 'GET', url: 'datacon/user_info.php', }).success(function (response) { $rootScope.userinfo = response[0]; console.log($rootScope.userinfo) $rootScope.$broadcast("userinfo", response[0]); })});app.controller('data', function ($scope, $http, $rootScope) { $scope.usernamea=''; $scope.$on("userinfo", function (event, msg) { console.log(msg); if(msg){ $scope.usernamea = msg.user; console.log($scope.usernamea) alert($scope.usernamea) } else{ alert(msg) } });});
Title, why doesn't the code on the inside run once every time the page is loaded?
Reply content:
var app = angular.module('gzmu', ["ngRoute",'chart.js']);app.run(function ($rootScope, $http) { $http({ method: 'GET', url: 'datacon/user_info.php', }).success(function (response) { $rootScope.userinfo = response[0]; console.log($rootScope.userinfo) $rootScope.$broadcast("userinfo", response[0]); })});app.controller('data', function ($scope, $http, $rootScope) { $scope.usernamea=''; $scope.$on("userinfo", function (event, msg) { console.log(msg); if(msg){ $scope.usernamea = msg.user; console.log($scope.usernamea) alert($scope.usernamea) } else{ alert(msg) } });});
Title, why doesn't the code on the inside run once every time the page is loaded?
The main reason is that in the app run broadcast is actually an asynchronous operation, if the data controller in run the request succeeds and broadcast before the creation, it will be successful call, otherwise it can not receive the root broadcast information