Arrays 调用$http时返回空数组。请在服务中获取它

Arrays 调用$http时返回空数组。请在服务中获取它,arrays,angularjs,service,promise,Arrays,Angularjs,Service,Promise,我想从从从$http.get生成的数组中获取名称,但是这返回的是一个空数组。当我执行控制台操作时,它会看到填充的数组。但是,当我在数组中循环以根据id是否等于某个值获取name属性的值时,数组为空 在我的控制器中,我有一个服务调用,它将返回name值 var params = { Id: $scope.Id, SettingId: $scope.SettingId }; $scope.selectedUserName = helloService.getSelectedUserName($sco

我想从从从$http.get生成的数组中获取名称,但是这返回的是一个空数组。当我执行控制台操作时,它会看到填充的数组。但是,当我在数组中循环以根据id是否等于某个值获取name属性的值时,数组为空

在我的控制器中,我有一个服务调用,它将返回name值

var params = { Id: $scope.Id, SettingId: $scope.SettingId };
$scope.selectedUserName = helloService.getSelectedUserName($scope.UserId, params);
为我效劳

I have used the getUserList function to populate the list of user in a dropdown and it works by generating the array with the values.

However When i got another page , i want to be able to display the name of the selected user, so I wanted to use the same getUserList function to retrieve the name

this.getUserList = function (val) {
        var usersObj = [];
        var url = "/api/v1/hello/getusers";
        var params = { Id: val.Id, SettingId: val.SettingId };
        var config = { params: params };
        var promise = $http.get(url, config)
            .then(function (response) {
                angular.forEach(response.data, function (key, value) {
                    angular.forEach(key, function (k, index) {
                     usersObj[index] = ({ userId: k.userId, name: k.name});
                  });
                });
            },
          function errorCallback(response) {
                    console.log("Unable to perform get request");
                    throw response;
                });
        var usersList = usersObj;

        return usersObj;

    };


 this.getSelectedUserName = function (id, param) {
        var name = "";
        var userList =this.getUserList(param);



        angular.forEach(userList, function (value, key) {
            if (value.userId == id)
                name = value.name;
        });
        return name;
}


数组长度为0,但如果在循环之前执行console.log(userList),数组将显示用户数据列表

 this.getSelectedUserName = function (id, param) {
        var name = "";
        var userList =this.getUserList(param);
        console.log(userList) ;
        angular.forEach(userList, function (value, key) {
            if (value.userId == id)
                name = value.name;
        });
        return name;
}


感谢您的友好回复

你可以这样试试。 这里我们使用的是
async
wait

服务

this.getUserList = function (val) {
        var usersObj = [];
        var url = "/api/v1/hello/getusers";
        var params = { Id: val.Id, SettingId: val.SettingId };
        var config = { params: params };
        return new Promise((resolve, reject) => {
           $http.get(url, config)
            .then(function (response) {
                angular.forEach(response.data, function (key, value) {
                    angular.forEach(key, function (k, index) {
                     usersObj[index] = ({ userId: k.userId, name: k.name});
                  });
                });
            },
            function errorCallback(response) {
                    console.log("Unable to perform get request");
                    throw response;
                });
            var usersList = usersObj;
            resolve(usersObj);
        });
    };


 this.getSelectedUserName = async function (id, param) {
        var name = "";
        var userList = await this.getUserList(param);
        console.log(userList);
        angular.forEach(userList, function (value, key) {
            if (value.userId == id)
                name = value.name;
        });
        return name;
}

让我知道它是否工作。

这是简单的Javascript,不是特定于Angular的。你能行

userList.forEach(user => {
    if(user.userId === id) {
         name = user.name;
    }
});
return name;
编辑: 如果您只想匹配用户数组中的一个id,您甚至不需要循环:

anArray = source.filter(source => source.toLowerCase().indexOf(id) === 0);
or
anObject = source.find(obj => obj.id === id);
这是哪个角度的版本?您的标签表示2.+但您有$scope,即ng1.x

既然已经有了阵列,为什么不能在视图中使用ngFor呢。您不需要在控件中对它们进行排序

组成部分

this.getSelectedUserName = function (id, param) {
    let name = ""; // should be array if you want to add unames to it
    let userList = this.getUserList(param);

    // what is `angular` here? And why loop here? Use ngFor in view.
    angular.forEach(userList, function (value, key) {
       if (value.userId == id){
           name = value.name; // will be overwritten each time
           // should be name.push(value.name); // but loop in view instead
       }
    });
    // this.users = name; // for your original sorted version
    this.users = userList;
}
在你看来

<li *ngFor="let user of users; index as i;>
  {{user.name}}
</li>

我明白你的建议,但我非常怀疑不止一个人会拥有相同的用户id,因此真的不需要数组。你只是想输出一个具有匹配id的用户?@BenRacicot是的,我想输入具有匹配id的用户。我使用的是angular 1.15版本。那么你就要使用
find()
返回不循环对象中的匹配项。如果您需要更多帮助,请告诉我。感谢您的回复我正在使用AngularJS v1.5.5,我已经尝试了代码,它正在用户界面上显示{},我的解决方案是否有效?