AngularJS $ scope在.then函数之外是未定义的

时间:2018-04-18 08:58:40

标签: html angularjs select ng-options

我有一个包含选择输入的表单,但是事情是ng-options无法正常工作

选择代码

<select  ng-model="selectedGender" ng-options="item.value for item in genderData">

我从

获得了数据
 ReferenceService.searchCategory("GENDER").then(function (response){

            $scope.genderData = response.data;
            console.log($scope.genderData);

        })

这是console.log($ scope.genderData)

Array(2)
0:{referenceId: 1, category: "GENDER", key: "GENDER_KEY", value: "Male", $$hashKey: "object:3"}
1:{referenceId: 2, category: "GENDER", key: "GENDER_KEY", value: "Female", $$hashKey: "object:4"}
length:2
__proto__
:
Array(0)

但我尝试过对数据进行硬编码

  $scope.genderData= [
            {
                "id": 1,
                "name": "Leanne Graham",
                "username": "Bret",
                "email": "Sincere@april.biz",
                "address": {
                    "street": "Kulas Light",
                    "suite": "Apt. 556",
                    "city": "Gwenborough",
                    "zipcode": "92998-3874",
                    "geo": {
                        "lat": "-37.3159",
                        "lng": "81.1496"
                    }
                },
                "phone": "1-770-736-8031 x56442",
                "website": "hildegard.org",
                "company": {
                    "name": "Romaguera-Crona",
                    "catchPhrase": "Multi-layered client-server neural-net",
                    "bs": "harness real-time e-markets"
                }
            } 
        ];

它有效!但是我使用了ng-options="item.name for item in genderData">

顺便提一下我刚刚搜索过的数据以便快速使用

编辑:

我想我发现了这个问题。为什么在函数外部未定义?

enter image description here

检查此控制台

enter image description here

第244行未定义但第242行不是?

2 个答案:

答案 0 :(得分:2)

将响应存储在var中,然后将该函数分配给$ scope。这是一个回调问题。

如果它仍然显示相同,您可以使用localsorage来存储和获取数据。但这种方法并没有在angularjs中重新编写。但如果没有任何反应,那么你也可以使用这个approcah

答案 1 :(得分:2)

那是因为ReferenceService.searchCategory("GENDER")会返回承诺

promise 将等待数据解析,然后转到 success 函数回调。哪个是.then(function(response))

因此代码将在承诺完成之前到达console.log244,并且$scope.genderData已创建,因此undefined

console.log上的242将一直等到promise已解决,并调用成功回调函数。

UPDATE:

以下是如何正确关联$scopefactory

的示例

请注意,您必须链接到object,而不是properties中的$scope

<强>错误:

$scope.gender = ReferenceService.data.genderData;

<强>正确:

$scope.gender = ReferenceService.data;

示例:

myApp.factory('ReferenceService',
    function ()
    {
        var ReferenceService = {
            // This is the global object. Link the $scope to this object
            data: {
                genderData: {}
            },
            searchCategory: function (type)
            {
                var getData = $http.get("URL", { params: { gender: type } }).then(
                    function (response)
                    {
                        // Store the data in the factory instead of the controller
                        // You can now link your controller to the ReferenceService.data
                        ReferenceService.data.genderData = response.data;
                    }
                )

                return getData;
            }
        }

        return ReferenceService;
    }
);

myApp.controller('MyController',
    function ($scope, ReferenceService)
    {
        // Link $scope to data factory object
        $scope.gender = ReferenceService.data;

        // Now you only have to call the factory function
        ReferenceService.searchCategory("GENDER");

        // This will now log the correct data
        console.log($scope.gender.genderData);
    }
)