Answer the question
In order to leave comments, you need to log in
AngularJS: how to update $scope after change in directive?
Hello.
Please tell me how to update the data in $scope after changing it inside the directive.
There is a controller in which the data from the database is initialized and displayed in the view:
$scope.info = {
users: info.users,
wells: info.wells,
revises: info.revises
};
.directive('daterangepicker', function($http) {
return {
restrict: 'A',
scope: {
options: '=daterangepicker',
start: '=dateBegin',
end: '=dateEnd'
},
link: function(scope, element, $scope, $rootScope) {
element.daterangepicker(scope.options, function(start, end) {
scope.start = start.format('D MMMM, YYYY');
scope.end = end.format('D MMMM, YYYY');
scope.$apply();
})
element.on('apply.daterangepicker', function(event, picker) {
console.log(picker.startDate.format('YYYY-MM-DD') + ' по ' + picker.endDate.format('YYYY-MM-DD'));
var start = picker.startDate.format('YYYY-MM-DD');
var end = picker.endDate.format('YYYY-MM-DD');
var vm = this;
vm.error;
$http.get('/api/v1/dashboard/'+start + '/' + end).success(function(info){
$scope.info = {
users: info.users,
wells: info.wells,
revises: info.revises,
flag: 1
};
console.log($scope.info);
vm.info = info;
}).error(function(error){
vm.error = error;
})
});
}
};
});
Инициализация: {users: 6, wells: 3, revises: 793}
Диапазон дат: 2017-12-01 по 2017-12-31
Новые данные: {users: 6, wells: 601, revises: 0, flag: 1}
element.on('apply.daterangepicker', function(event, picker) {
$scope.$apply(function() {
console.log(picker.startDate.format('YYYY-MM-DD') + ' по ' + picker.endDate.format('YYYY-MM-DD'));
var start = picker.startDate.format('YYYY-MM-DD');
var end = picker.endDate.format('YYYY-MM-DD');
var vm = this;
vm.error;
$http.get('/api/v1/dashboard/'+start + '/' + end).success(function(info){
$scope.info = {
users: info.users,
wells: info.wells,
revises: info.revises,
flag: 1
};
console.log($scope.info);
vm.info = info;
}).error(function(error){
vm.error = error;
})
});
});
Answer the question
In order to leave comments, you need to log in
use bidirectional data binding in the template
https://metanit.com/web/angular2/2.5.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question