【发布时间】:2013-10-02 16:52:10
【问题描述】:
我有一个简单的问题,我认为有一个非常简单的解决方案,但不知何故我错过了它。
我正在为我的应用设置一个简单的指令。我将范围属性设置为默认值“false”,因为我希望它共享我的控制器的范围。
问题是,我似乎无法访问该范围。
在我的链接功能中,我可以这样做:
console.dir(scope)
我可以在范围对象中看到我所追求的属性('pages')。
如果我尝试这样做:
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 是
undefined而不是空对象或数组吗?当您扩大范围以在控制台中查看 pages 属性时,它说明了什么?我看不出它应该说出与输出scope.pages不同的任何理由,所以我的回答会假设您实际上看到的是一个空对象或数组。
标签: javascript angularjs-directive