약속이 해결되기 전에 지시문이 렌더링됩니다.
약속이 해결 된 후에 만 콘텐츠를 렌더링하라는 지시를받는 데 문제가 있습니다. 이렇게 then()
해야한다고 생각 했는데 안되는 것 같네요 ..
내 컨트롤러는 다음과 같습니다.
// Generated by CoffeeScript 1.6.3
(function() {
var sprangularControllers;
sprangularControllers = angular.module('sprangularControllers', ['sprangularServices', 'ngRoute']);
sprangularControllers.controller('productsController', [
'$scope', '$route', '$routeParams', 'Product', 'Taxonomy', function($scope, $route, $routeParams, Product, Taxonomy) {
Taxonomy.taxonomies_with_meta().$promise.then(function(response) {
return $scope.taxonomies = response.taxonomies;
});
return Product.find($routeParams.id).$promise.then(function(response) {
return $scope.currentProduct = response;
});
}
]);
}).call(this);
내 지시 :
// Generated by CoffeeScript 1.6.3
(function() {
var sprangularDirectives;
sprangularDirectives = angular.module('sprangularDirectives', []);
sprangularDirectives.directive('productDirective', function() {
return {
scope: {
product: '='
},
templateUrl: 'partials/product/_product.html',
link: function(scope, el, attrs) {
console.log(scope);
console.log(scope.product);
return el.text(scope.product.name);
}
};
});
}).call(this);
범위는 괜찮아지고, 개발 도구에서 확인할 때 scope.product
정의되지 않은 것은 아니지만 확인 할 때 약속이 해결 되었기 때문이라고 생각합니다.
console.log(scope.product)
그러나 undefined를 반환합니다 ..
값이 비동기 적으로 채워 지므로 바인딩 된 요소를 업데이트하는 감시 함수를 추가해야합니다.
link: function(scope, el, attrs) {
scope.$watch('product', function(newVal) {
if(newVal) { el.text(scope.product.name);}
}, true);
}
또한 많은 복잡성을 지시어 컨트롤러로 옮기고 링크 기능을 사용하여 DOM 만 조작 할 수 있습니다.
The true
third parameter to $watch
causes a deep watch, since you're binding this directive to a model.
Here are a couple of links with good examples:
http://www.ng-newsletter.com/posts/directives.html
http://seanhess.github.io/2013/10/14/angularjs-directive-design.html
As stated in an official thread about this issue (quickly closed as "won't fix because it would make directives wait"), a workaround is to wrap your directive in a ng-if
:
<div ng-if="myPromiseParam">
<my-directive param="myPromiseParam">
</div>
I know this is an older question, but thought I would try my hand at providing an updated answer.
When using a router, both ui-router and ngRouter have resolve methods that will resolve promises on url changes before switching to that route and rendering things on the page.
ngRouter Resolve tutorial
ui-router Resolve docs
Another option, instead of using $watch
is to use angulars $q
promise library. More specifically, the $q.when()
method. This takes promises and values. IF it's a promise it will fire a .then()
when the promise resolves. If its a value, it wraps it in a promise and immediately resolves it.
link: function(scope, el, attrs){
$q.when(scope.product).then(function(product){
scope.product = product;
el.text(scope.product.name);
});
}
Or there a couple way you can not show anything just with html.
<product-directive product='object'>
<!-- Will hide span until product.name exists -->
<span ng-show='product.name'>{{ product.name }}</span>
<!-- Will show default text until key exists -->
{{ product.name || 'Nothing to see here' }}
<!-- Will show nothing until key exists -->
<span ng-bind='product.name'></span>
</product-directive>
use $watch on your variable in your directive to get the updated value of your variable.
you can also make use of $q to resolve the promise.
참고URL : https://stackoverflow.com/questions/21177582/directive-is-being-rendered-before-promise-is-resolved
'Programing' 카테고리의 다른 글
JavaScript는 어떤 언어로 작성됩니까? (0) | 2020.12.07 |
---|---|
사용자가 알림을 닫지 못하도록 방지 (0) | 2020.12.07 |
모든 관계를 포함하여 Eloquent 객체를 복제 하시겠습니까? (0) | 2020.12.07 |
JavaScript onClick 이벤트 핸들러에서 큰 따옴표 이스케이프 (0) | 2020.12.07 |
문자열을 "XML 안전"으로 만드는 방법은 무엇입니까? (0) | 2020.12.07 |