从函数

时间:2015-10-31 19:10:26

标签: angularjs

我想在函数中为全局变量$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);


});

0 个答案:

没有答案