我想在函数中为全局变量$scope.username
分配user.name
的值,但是在页面底部的console.log
中我得到null,我做错了什么?
angular.module('testCon').controller('UserController', function ($scope, UserService, $location) {
$scope.users = [];
$scope.theUser = [];
$scope.username = '';
UserService.getAll().then(
function (data) {
$scope.users = data;
}, function (err) {
console.log(err);
}
);
$scope.test = function(){
$scope.users.forEach(function (user) {
if(user.id == $scope.id && user.name == $scope.name){
$scope.username = user.name;
console.log($scope.username);
}
});
}
console.log($scope.username);
});