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;
});
}
});