Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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_Ajax_Json_Angularjs_Angularjs Resource - Fatal编程技术网

Javascript 如何等待AngularJS中的状态更改?

Javascript 如何等待AngularJS中的状态更改?,javascript,ajax,json,angularjs,angularjs-resource,Javascript,Ajax,Json,Angularjs,Angularjs Resource,使用Angular 1.2.3,我提供了如下服务: var itemService = function ($resource) { return $resource('/rest/item', {action: ''}, { query: {method: 'GET'} }); }; services.factory('Item', ['$resource', itemService]); 在我的控制器中,我想保存一个项目,所以我这样做了 $scope.ite

使用Angular 1.2.3,我提供了如下服务:

var itemService = function ($resource) {
    return $resource('/rest/item', {action: ''}, {
        query: {method: 'GET'}
    });
};
services.factory('Item', ['$resource', itemService]);
在我的控制器中,我想保存一个项目,所以我这样做了

$scope.item = Item.query(); // this returns 1 item
$scope.puppet.$save(); // this results in the desired POST on my service
POST处理程序生成的图像可能需要一些时间。我知道$save调用会立即返回,因为调用是异步的。 一旦POST处理程序完成,它将返回一个带有布尔calculationsDone=true的项,这将反映在$scope中

<div ng-show="item.calculationsDone"><img ng-src="{{src/of/image.jpg}}"/></div>

现在我想显示生成的图像。我正在使用上面的html。我已经知道生成的图像的URL,但是我不能把它放在ng src中,因为它还没有解析

如何在项目状态更改时更新或重新应用ng src


我需要承诺吗?还是$watch?

像这样的东西怎么样

$scope.imageSrc = "";
scope.puppet.$save(function(value, responseHeaders) {
 $scope.imageSrc = "src/of/image.jpg";
})

<img ng-src="{{imageSrc}}"/>
$scope.imageSrc=”“;
scope.puppet.$save(函数(值、响应头){
$scope.imageSrc=“src/of/image.jpg”;
})
我猜另一个选项是图像元素的ng if=“item.calculationsDone”。 一旦表达式的计算结果为true,img元素就会附加到DOM。
在此之前,它不是DOM的一部分->图像也不会被加载。

我最终选择了ng if。谢谢