我创建了一个指令,在标签内加载一些 html 代码:
指令:
var appDirectives = angular.module("skdApp", ["offerServices"]);
appDirectives.directive("widget", function() {
return {
restrict: 'A',
template: '<div ng-controller="OfferCtrl"><p>{{offer.price}}</p></div>';
}
服务
此外,我正在使用服务来获取一些 json 数据
angular.module('offerServices', ['ngResource']).
factory('Offer', function ($resource) {
return $resource('url/offer.json', {},
{
get: {method: 'JSONP', params: {callback: 'JSON_CALLBACK'}}
}
);
});
Controller
Controller 将接收到的数据添加到范围
function OfferCtrl($scope, Offer) {
$scope.offer = Offer.get();
}
现在我面临以下问题。当模板加载时,json 请求尚未完成。这会导致模板显示的 {{offer.price}} 为空字符串。
一旦 OfferServices 收到响应(即 OfferService 响应可用),我需要一种方法来使用相应的值更新模板
有什么想法吗?
最佳答案
使用 ngCloak 怎么样?
Description
The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.
The directive can be applied to the element, but typically a fine-grained application is prefered in order to benefit from progressive rendering of the browser view.
关于javascript - angular.js - 在 jsonp 请求后更新范围和模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13141816/