指令是在承诺解决之前呈现的
我有问题得到我的指示,只有在我的承诺解决后,才能呈现其内容。 我以为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 .. 
由于您的值是asynchronous填充的,因此您需要添加一个更新绑定元素的监视函数。
  link: function(scope, el, attrs) { scope.$watch('product', function(newVal) { if(newVal) { el.text(scope.product.name);} }, true); } 
您也可以将很多复杂性转移到指令控制器中,并仅使用链接function来操作DOM。
  $watch true第三个参数引起了深入的观察,因为你将这个指令绑定到模型上。 
 以下是一些有很好例子的链接: 
  http://www.ng-newsletter.com/posts/directives.html 
  http://seanhess.github.io/2013/10/14/angularjs-directive-design.html 
 正如关于这个问题的一个官方线程 (很快closures,因为“不会修复,因为它会使指令等待”)中所述,解决方法是将您的指令包装在ng-if : 
 <div ng-if="myPromiseParam"> <my-directive param="myPromiseParam"> </div> 
我知道这是一个较老的问题,但是我想我会试着提供一个更新的答案。
当使用路由器时,ui-router和ngRouter都有parsing方法,在切换到该路由并在页面上显示内容之前,这些方法将parsingURL更改的承诺。
  ngRouter解决教程 
  ui-router解决文档 
 另一个select,而不是使用$watch是使用angulars $q promise库 。 更具体地说, $q.when()方法。 这需要承诺和价值观。 如果这是一个承诺,它会触发一个.then()当承诺解决。 如果它的价值,它包装在一个承诺,并立即解决它。 
 link: function(scope, el, attrs){ $q.when(scope.product).then(function(product){ scope.product = product; el.text(scope.product.name); }); } 
或者有一些方法,你不能用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> 
在你的指令中使用$ watch来获取variables的更新值。
你也可以利用$ q来解决这个承诺。