Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS中的承诺错误_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS中的承诺错误

Javascript AngularJS中的承诺错误,javascript,angularjs,Javascript,Angularjs,我承诺从我的服务中的json获取数据,然后将其传递给我的控制器。我在这方面遇到了一些问题 这是我的服务 getMetaData: function () { var defer = this.$q.defer(); this.$http.get('http://localhost:8084/login-api/public/metadata/describe/Coisa') .success(function

我承诺从我的服务中的json获取数据,然后将其传递给我的控制器。我在这方面遇到了一些问题

这是我的服务

getMetaData: function () {
            var defer = this.$q.defer();
            this.$http.get('http://localhost:8084/login-api/public/metadata/describe/Coisa')
                    .success(function (data, status, headers, config) {
                        defer.resolve(data)
                    })
                    .error(function (data, status, headers, config) {
                        defer.reject(status);
                    })
            return defer.promise;

        }
这里我正在呼叫服务,我是我的控制器

getData: function () {
            this.$scope.meta = this.EntityService.getMetaData();
            var dataS = {};
            this.$scope.meta.then(
                    function (data) {
                        this.$scope.metaD = data;
                    },
                    function (err) {
                        alert('Error');
                    }
            )
        }
这就是我所犯的错误:

TypeError: Cannot set property 'metaD' of undefined
at basic-list-controller.js:29
at l.promise.then.l (angular.js:7)
at angular.js:7
at u.$get.u.$eval (angular.js:7)
at u.$get.u.$digest (angular.js:7)
at u.$get.u.$apply (angular.js:7)
at p (angular.js:7)
at T (angular.js:7)
at XMLHttpRequest.b.onreadystatechange (angular.js:7)

我不明白为什么我会犯这个错误。当我在控制台上记录数据时,数据就在那里,但我不能给变量赋值。

您可以将其简化为这一点,因为使用$http服务可以直接返回承诺,并在返回数据之前使用“then”函数预处理数据:

getMetaData: function () {
            return $http.get('http://localhost:8084/login-api/public/metadata/describe/Coisa')
                    .then(function (success) {
                        return success.data;
                    },
                      function (error) {
                        return error;
                    });
        }
现在,这应该适用于代码的其余部分。注意:我返回整个结果,如果您只需要响应,则返回“succes.data”。或者,您可以在控制器代码中检查它。

在成功函数中,“this”不是您所期望的

function (data) {
    this.$scope.metaD = data;
}
您可以尝试在回调外部进行如下设置:

var _this = this;
在回调函数中使用:

function (data) {
    this.$scope.metaD = data;
}
您的最终代码应该如下所示:

getData: function () {
        var _this = this;
        this.$scope.meta = this.EntityService.getMetaData();
        var dataS = {};
        this.$scope.meta.then(
                function (data) {
                    _this.$scope.metaD = data;
                },
                function (err) {
                    alert('Error');
                }
        )
    }

你处理这件事的方式有很多问题

首先:对于已经返回承诺的内容,例如
$http
,您不需要使用
$q.defer
——这被认为是一个错误

第二步:避免将
$scope
传递给您的服务-这会使测试更加困难,并扰乱关注点的分离<代码>$scope属于控制器

因此,您的服务可以变得更简单。您甚至不需要两个方法,因为它们都返回相同的内容

.factory("myDataService", function($http){
    return {
       getMetaData: function(){
          return $http.get('url/to/data');
       }
    };
}


.controller("MainCtrl", function($scope, myDataService){
   myDataService.getMetaData()
      .then(function(data)){
         $scope.metaD = data;
      });
}

这。$scope.metaD=数据;你为什么到处都使用
这个。
?为什么要使用
this.$scope
而不是
$scope
@WayneEllery,为什么要在内部使用
$scope
service@NewDev我认为OP在控制器中只使用了$scope,尽管它并不完全正确clear@WayneEllery,我似乎很清楚,它在服务定义
中。然后这里的
是完全无用的-它没有做任何事情。您的权利,上面的示例没有显示其好处,但我通常使用此技术仅返回响应或在返回前预解析数据。因此,调用者在其“then”呼叫中获得已处理的数据。虽然这可能解决OP的特定错误,但您的回答会导致在服务内部使用
$scope
时问题的不正确分离。这使得该服务的可重用性降低,或者至少降低了可测试性。是的,我同意你的看法。我只想回答具体的问题。非常感谢,这为我解决了问题。谢谢你关于模式的建议和提示,我会读更多的。我是AngularJs的新手!