我在解決我的承諾後才能獲取我的指令以呈現其內容。我以爲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