我试图输入我的内容但无法显示。当我刷新时会显示不同的东西...... Anyones可以帮我解决这个问题吗?
<script src="./angular/angular.js"></script>
<script src="./angular/angular-cookies.js"></script>
<script>
angular.module('cookieStoreExample', ['ngCookies'])
.controller('ExampleController',['$scope', '$cookieStore', function($scope, $cookieStore){
var exprs = $scope.exprs = [$cookieStore.get('myExprs',$scope.exprs)];
$scope.addExp = function(exprs) {
exprs.push(exprs);
};
$scope.removeExp = function(index) {
exprs.splice(index, 1);
};
$cookieStore.put('myExprs',$scope.exprs);
var favoriteCookie = $cookieStore.get('myExprs',$scope.exprs);
}])
</script>
答案 0 :(得分:1)
这个例子似乎有几个问题:
$cookieStore.get()
已经返回一个数组,不需要[]&#39; s。addExp()
的参数为exprs
,隐藏了exprs
数组。$cookieStore.put()
来保持更改。更新的代码:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-cookies.js"></script>
<script>
angular.module('cookieStoreExample', ['ngCookies'])
.controller('ExampleController', ['$scope', '$cookieStore', function ($scope, $cookieStore) {
var exprs = $scope.exprs = $cookieStore.get('myExprs');
if (!angular.isArray(exprs)) {
exprs = [];
}
$scope.addExp = function (expr) {
exprs.push(expr);
$cookieStore.put('myExprs', exprs);
};
$scope.removeExp = function (index) {
exprs.splice(index, 1);
$cookieStore.put('myExprs', exprs);
};
$scope.favoriteCookie = $cookieStore.get('myExprs');
}])
</script>