我正在尝试放入我的内容,但无法显示。当我刷新时会显示不同的东西...有人能帮我解决这个问题吗?
<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>发布于 2015-03-31 03:33:08
这个例子似乎有几个问题:
$cookieStore.get()已经返回了一个数组,不需要[]的。addExp()的exprs,因此需要调用exprs 来保持更改。
更新代码:
<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>https://stackoverflow.com/questions/29339562
复制相似问题