I have created a controller and directive in my angular application defined as following:
app.controller('CrudCtrl', ['$scope', '$http', function($scope, $http) {
$scope.isLoading = true;
$scope.pageChanged = function(){
$http({
method: 'GET',
url: 'http://localhost:8080/rest/repository'+$scope.repository.path,
params: {
size: 3,
page: $scope.currentPage
}
}).
success(function (data, status){
$scope.rowCollection = data._embedded['rest-original-product'];
$scope.totalPages = data.page.totalPages;
$scope.currentPage = data.page.number + 1;
$scope.totalElements = data.page.totalElements;
$scope.pageSize = data.page.size;
$scope.isLoading = false;
$scope.numPages = 5;
}).
error(function (data, status){
log.error("Error");
});
};
$scope.$watch('repository', function(newVal, oldVal){
if(newVal!=null && oldVal!=newVal ){
}
});
}]);
app.directive('crud', ['$resource', 'CrudConstant', '$http', function($resource, CrudConstant, $http) {
return {
//scope:{},
link: function(scope, element, attrs) {
scope.repository = CrudConstant[attrs.repository];
$http({
method: 'GET',
url: 'http://localhost:8080/rest/repository'+scope.repository.path,
params: {
size: 3
}
}).
success(function (data, status){
scope.rowCollection = data._embedded['rest-original-product'];
scope.totalPages = data.page.totalPages;
scope.currentPage = data.page.number + 1;
scope.totalElements = data.page.totalElements;
scope.pageSize = data.page.size;
scope.isLoading = false;
scope.numPages = 5;
}).
error(function (data, status){
log.error("Error");
});
},
templateUrl: 'tpl/app/crud/crud.html'
};
}]);
app.directive('table', ['$resource', function($resource) {
return {
link: function(scope, element, attrs) {
},
templateUrl: 'tpl/app/crud/table.html'
};
}]);
while the structure is the following:
<div ui-view class="fade-in-up" ng-controller="CrudCtrl">
<crud repository="originalProduct">
</crud>
</div>
where here goes crud template:
<div class="col-sm-5 m-b-xs">
<pagination
total-items="totalElements"
items-per-page="pageSize"
ng-change="pageChanged()"
ng-model="currentPage"
max-size="5"
class="pagination-sm m-t-none m-b"
boundary-links="true"
rotate="false"
num-pages="numPages"></pagination>
</div>
<div>{{rowCollection}}</div>
When I launch the application rest call is executed correctly and data are shown as expected (rowCollection shows correct data), but when I click on the pagination button which calls pageChanged() function, the controller has not $scope.repository property set. What seems to be happening is that controller is not binded/linked to the scope defined in the directive.
I also tried to add scope.$apply() at the end od the link function in the directive but I get error in that case: "Error: [$rootScope:inprog] $digest already in progress".
isolatedscope? seems like commented for some reason or is there?require: '^'option of directive