我正在使用Typescript 2.1(开发人员版本)将async/await转换为ES5.
我注意到在我更改了在异步函数中绑定查看的任何属性后,视图不会使用当前值更新,所以每次我必须在函数结束时调用$ scope.$ apply().
示例异步代码:
async testAsync() {
await this.$timeout(2000);
this.text = "Changed";
//$scope.$apply(); <-- would like to omit this
}
Run Code Online (Sandbox Code Playgroud)
此后text,视图中不会显示新值.
是否有任何解决方法,所以我不必每次手动调用$ scope.$ apply()?
我正在尝试使用Pinterest JavaScript SDK来获取项目的一些引脚数据.我在我创建的Pinterest服务中有一个方法可以在我的HomeController中调用.我尝试在响应中抛出响应,所以我可以将它放在我的HomeController上$scope并在我的视图中显示它.但是,在我看来,$ scope.pins是未定义的.为什么undefined?似乎承诺正在发挥作用.还在学习承诺.
function getBoardPins (id) {
return new Promise(function (resolve, reject) {
PDK.request('/v1/boards/' + id + '/pins/', 'GET', {fields: 'id,link,url,creator,board,created_at,note,color,counts,media,attribution,image,metadata'}, function (response) {
if (response) {
resolve(response);
}
reject();
});
});
}
Run Code Online (Sandbox Code Playgroud)
Pinterest.getBoardPins('490329546869188172').then(function (response) {
$scope.pins = response.data;
});
Run Code Online (Sandbox Code Playgroud)
<h1>Pinterest</h1>
<div ng-repeat="pin in pins">
<div>{{pin}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)