An angularjs directive have a scope value from the declared controller. Suppose your need a global access meas using $rootScope declaration to access all controller from the application.
$localStorage is long time storage a browser data, It will reuse the data. This localStorage is declare into ngStorage.js library.
HTML Code:
<div ng-app="Loc">
<div ng-controller="LocCtrl">
<div ng-click="clickCount()">
Count : <span>{{count}}</span>
</div>
</div>
</div>
<div ng-controller="LocCtrl">
<div ng-click="clickCount()">
Count : <span>{{count}}</span>
</div>
</div>
</div>
JS code:
var app = angular.module("Loc",['ngStorage']);
app.controller('LocCtrl', function($scope, $localStorage){
//One time local storage logic
if(angular.isUndefined($localStorage.storeCount)){
console.log('Local storage data one time set');
$localStorage.storeCount = 0;
}
$scope.count = $localStorage.storeCount;
// Click Logic
$scope.clickCount = function(){
$scope.count = $localStorage.storeCount;
$scope.count = $scope.count + 1;
$localStorage.storeCount = $scope.count;
}
});
Result:
Comments
Post a Comment