Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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
Angularjs 如何将承诺分配给范围_Angularjs_Restangular - Fatal编程技术网

Angularjs 如何将承诺分配给范围

Angularjs 如何将承诺分配给范围,angularjs,restangular,Angularjs,Restangular,如何将$promise分配给$scope.advertizername?在下面的示例中,Console.log($scope.title)返回“undefined” 如果我没有弄错,这是因为异步调用 异步意味着从正常执行流中删除发送请求(或者更确切地说接收响应)。以你为例,, $.ajax立即返回,下一条语句return result;,是 在作为成功回调传递的函数被调用之前执行 打电话来 你应该这样做 $scope.someFunction = function () { Restangul

如何将$promise分配给$scope.advertizername?在下面的示例中,Console.log($scope.title)返回“undefined”


如果我没有弄错,这是因为异步调用

异步意味着从正常执行流中删除发送请求(或者更确切地说接收响应)。以你为例,, $.ajax立即返回,下一条语句return result;,是 在作为成功回调传递的函数被调用之前执行 打电话来

你应该这样做

$scope.someFunction = function () {
 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
   return $scope.advertiserName = data.name;
 });
}
$scope.title = $scope.someFunction(); //It will give you output
编辑1:

我读了很多关于这方面的文章,我注意到,
异步
调用的响应将从正常的执行流中删除。使用
restangule
$http
调用,两者都是
异步调用。所以编译器不会等待您的响应。您需要告诉编译器等待ajax响应。我在我的一个项目中所做的。这里有一个简单的例子,可以说明更多

首先,我声明了一个控制器函数。如下

$scope.asynchronousCallee = function (){
 $http.get('/url').
  success(function(data, status, headers, config) {
    $scope.myAjaData = data;
  });
}

$scope.asynchronousCallee(); //Call above function just after the declaration

简单地说,此函数将通过
get
调用从服务器接收数据,并在变量中分配响应,但请注意,此成功函数将异步调用
。因此,我所做的就是在声明函数之后调用
asynchronousCallee
函数。现在,编译器将等待该函数的响应,在执行该函数后,编译器将继续进行。我希望它能帮助你,兄弟:-)

在得到ajax调用的响应后,我们可以使用回调函数来执行代码行。你可以访问这个博客了解回调函数的精彩解释。让我们通过代码来理解它

    $scope.setNameId = function (title,id,callBackFunction) {
     Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
      // $scope.advertiserName = data.name;
       $scope.title= data.name;
      // $scope.advertiserId = data.id;
       $scope.id=data.id;
       callBackFunction();
     });
    }
     $scope.setNameId($scope.title,$scope.id,executeAfterResponse);
    var executeAfterResponse=function(){
    //code that you want to execute when value of $scope.title and $scope.id has changed
    };
我们也可以通过这种方法做到这一点

$scope.setNameId(executeAfterResponse);
无需在
$scope.setNameId
函数的参数中传递
$scope.title
$scope.id
变量,即可在同一文件中直接访问
$scope
变量


注释的代码行不是必需的,因为我们直接为
$scope.name
$scope.id
赋值。

在下面的示例中,您希望维护广告客户名称和标题以及广告客户id和id的内存引用。然而,当从作用域中提取属性时,它是通过值而不是引用来检索的。如果您想让代码正常工作,您必须做以下两件事之一:

 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
   $scope.advertiserName = data.name;
   $scope.advertiserId = data.id;
 });

 $scope.title = $scope.advertiserName;
 $scope.id = $scope.advertiserId;
初始化作用域上的正确属性:

 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
     $scope.title = data.name;
     $scope.id = data.id;
 });
将其改为按引用更新:

 var advertiser = {
     id: $scope.advertiser,
     title: $scope.advertiser
 }
 $scope.advertiser = advertiser;

 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
     advertiser.title = data.name;
     advertiser.id = data.id;
 });

由于通过使用angular的承诺已经触发了一个摘要周期,因此视图将被更新

正如您所说,我实际上在寻找同步调用。谢谢你,维尼特。但是$scope.title是在$scope.someFunction完成之前执行的。我还想在执行$scope.scomeFunction之后执行scope.title旁边的代码。@无法访问,请检查我编辑的答案。这对我很有效。我希望它也能对你有用:-)你问什么都不清楚。是否要将
$promise
分配给
广告商名称
?现在,您可能已经正确地将结果分配给了
广告商名称
;这里有什么问题?
 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
     $scope.title = data.name;
     $scope.id = data.id;
 });
 var advertiser = {
     id: $scope.advertiser,
     title: $scope.advertiser
 }
 $scope.advertiser = advertiser;

 Restangular.all('advertiser').one("?id=" + 1).getList().then(function(data) {
     advertiser.title = data.name;
     advertiser.id = data.id;
 });