我有一个简单的问题,我认为有一个非常简单的解决方案,但不知何故,我错过了它。AngularJS:指令和范围
我为我的应用程序设置了一个简单的指令。我将scope属性设置为默认值“false”,因为我希望它共享我的控制器的范围。
问题是,我似乎无法访问该范围。
在我的链接功能,我可以这样做:
console.dir(scope)
,我可以看到我之后的范围对象(“页”)的财产。
如果我尝试这样做:
console.dir(scope.pages)
然而,它回来为未定义。
我错过了什么?
在此先感谢。
MyDirectives.directive("mdPagination", function(){
return {
templateURL: "/templates/pagination.html",
replace: true,
restrict: 'A',
scope: false, //default
link: function(scope, element, attrs){
console.log('in linking function');
console.dir(scope);
console.dir(element);
console.dir(attrs);
console.dir(scope.pages);
}
}
});
模板:
<nav class="pagination" md-Pagination></nav>
控制器:
App.controller('MachinesListCtrl', function ($scope, $routeParams, $location, MachineServices, CustomerServices) {
var page = ($routeParams.page ? $routeParams.page : 1);
//if we are getting all machines based on their customer id.
if($routeParams.customerID){
MachineServices.getByCustomerId($routeParams.customerID).then(function (data) {
_.each(data.results, function (machine, index, list) {
CustomerServices.get(machine.customer_id).then(function(data){
machine.CustomerData = data.results;
});
});
$scope.Machines = data.results;
$scope.pages = data.pages;
});
}
//otherwise we just want the regular list of machines.
else{
MachineServices.query(page).then(function (data) {
_.each(data.results, function (machine, index, list) {
CustomerServices.get(machine.customer_id).then(function(data){
machine.CustomerData = data.results;
});
});
$scope.Machines = data.results;
$scope.pages = data.pages;
});
}
});
能否请你告诉模板的代码中使用的指令?和控制器在哪里设置scope.pages的值? –
你确定* scope.pages是未定义的,而不是空对象或数组?当您扩展范围以查看控制台中的页面属性时,它说了什么?我看不出有什么理由说它应该说出与输出'scope.pages'不同的东西,所以我的答案会假设你实际上看到了一个空的对象或数组。 –