我在解决我的承诺后才能获取我的指令以呈现其内容。我以为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 ..
谢谢吉姆,这是否意味着它只是没有更新的隔离范围?使用手表确实困扰了我,但是我没有在网上看到任何例子,所以我推测这不是'有角度的方式'......希望事实并非如此,会给出一个结果并报告回来。 – Melbourne2991
我不明白你的问题。指令都是控制器范围内的嵌套作用域。如果你没有明确定义一个范围,那么angular会创建一个。它也会在后台为你创建手表(比如当你做'{{project.name}}')时,所以我会说手表是最有角度的。另外,[掌握使用AngularJS进行Web应用程序开发| Packt Publishing](http://bit.ly/1deKEKU)是一本好书。 –
如果有人来此。 $ watch也可以在父控制器中。由于该指令绑定到相同的范围值。此外,如果您在获取新对象点注释值的对象上有手表,那么您应该检查它是否存在于手表中。 – mjwrazor