阵列像静态可能的角度JS? [英] array like static possible in angular js?
问题描述
我要声明一个数组
和我需要保持添加的每个项目应该继续是永远在,即使页面重新加载,这可能吗?
VAR ARR = [];
arr.push('身份证');
and i need to keep each item added to it should remain for there for ever, even if the page is reloaded, is it possible? var arr=[]; arr.push('id');
这样,但存储的项目不应该被取代。
like this, but stored item should not be replaced.
推荐答案
您可以使用cookies AngularJS保持值。
仅供参考,请这个使用$的CookieStore,两个控制器,一个$ rootScope和AngularjS 1.0.6的链接描述herea的jsfiddle。这是对的jsfiddle作为 https://jsfiddle.net/h2fs0han/ 作为基础,如果你瞎搞与这...
You can use cookies in AngularJS to maintain the values. FYI, enter link description herea JSFiddle of this using the $cookieStore, two controllers, a $rootScope, and AngularjS 1.0.6. It's on JSFiddle as https://jsfiddle.net/h2fs0han/ as a base if you're messing around with this...
有关更多信息,请参阅 - 如何访问cookies在AngularJS
For more information refer - How to access cookies in AngularJS?
<div ng-app="myApp">
<div id="lastVal" ng-controller="ShowerCtrl">{{lastVal}}</div>
<div id="button-holder" ng-controller="CookieCtrl">
<input type="text" ng-model="strText" />
<button ng-click="AddItem()">Add Item!</button>
</div>
</div>
var myApp = angular.module('myApp', ['ngCookies']);
myApp.controller('CookieCtrl', function($scope, $rootScope, $cookieStore) {
$scope.strText = "";
$scope.AddItem = function() {
var lastVal = $cookieStore.get('lastValue');
if ($scope.strText) {
if (!lastVal) {
$rootScope.lastVal = new Array(); //Default
$rootScope.lastVal.push($scope.strText);
} else {
var newItem = angular.copy(lastVal);
newItem.push($scope.strText);
$rootScope.lastVal = newItem;
}
$cookieStore.put('lastValue', $rootScope.lastVal);
}
};
});
myApp.controller('ShowerCtrl', function() {});
这篇关于阵列像静态可能的角度JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!