使用 ng-class 基于 ng-cookies 存储的数据
Using ng-class based on ng-cookies stored data
我正在尝试根据函数内的条件使用 ng-class,但有时切换不起作用。我使用了 $scope.getBookmarkIcon 函数,这样即使在页面刷新后我也可以使用 bookmarkIcon class 选择 ng-class 但它不起作用 属性。
$scope.bookmarks = $cookieStore.get('data_bookmark') ? $cookieStore.get('data_bookmark') : { items:[] };
console.log($scope.bookmarks);
$scope.saveBookmark = function(resortId) {
$scope.bookmarks.items.push({ id:resortId });
$cookieStore.put('data_bookmark',$scope.bookmarks);
var cookie = $cookieStore.get('data_bookmark');
console.log($scope.bookmarked);
console.log(cookie);
};
$scope.removeBookmark = function(resortId) {
var data = $scope.bookmarks.items;
for(var key in data) {
if(data[key].id===resortId) {
var index = Object.keys(data).indexOf(key);
$scope.bookmarks.items.splice(index, 1); break;
}
}
$cookieStore.put('data_bookmark',$scope.bookmarks);
var ncookie = $cookieStore.get('data_bookmark');
console.log($scope.bookmarked);
console.log(ncookie);
};
$scope.getBookmarkIcon = function(resortId)
{
$scope.bookmarked = false;
var dataList = $scope.bookmarks.items;
dataList.filter(function(item) {
if(item.id===resortId) {
$scope.bookmarked = true;
}
});
return $scope.bookmarked;
};
}]);
这里是我到目前为止的尝试:
http://plnkr.co/edit/raZeNb9FdCtfsf6FanU5?p=preview
终于解决了我的问题,分享一下。
这是完整的来源:
http://plnkr.co/edit/9NZHjAuEvC6wVN7YE8Yh?p=preview
我用一个切换功能简化了它,而不是两个功能,一个用于添加书签,另一个用于删除书签。
我初始化了
$scope.bookmarked = 错误;
然后在 html:
<td class="ellipsis" ng-model="bookmarked" ng-class="{'bookmarkIcon':getBookmarkIcon(item)}">
<a ng-click="bookmarked=!bookmarked; toggleBookmark(item);"
href="javascript:void(0);" title="{{(getBookmarkIcon(item))?'Remove
bookmark':'Bookmark this area'}}">{{item}}
</a>
</td>
然后我将 getBookmarkIcon(item) 保留在模型之外,其中仅根据 cookie 中的存储值返回 true 或 false。
var app = angular.module('myCookieBasedBookmark', ['ngCookies']);
app.controller('homeController', ['$cookieStore', '$cookies', '$scope', function($cookieStore, $cookies, $scope) {
$scope.bookmarked = false;
$scope.bookmarks = $cookieStore.get('data_bookmark') ? $cookieStore.get('data_bookmark') : { items:[] };
console.log($scope.bookmarks);
$scope.toggleBookmark = function(resortId) {
var data = $scope.bookmarks.items;
var found = false;
for(var key in data) {
if(data[key].id===resortId) {
var index = Object.keys(data).indexOf(key);
$scope.bookmarks.items.splice(index, 1); found = true; break;
}
}
if(!found){
$scope.bookmarks.items.push({ id:resortId });
}
$cookieStore.put('data_bookmark',$scope.bookmarks);
var cookie = $cookieStore.get('data_bookmark');
console.log(cookie);
};
$scope.getBookmarkIcon = function(resortId) {
var found = false;
var dataList = $scope.bookmarks.items;
dataList.filter(function(item) {
if(item.id===resortId) {
found = true;
}
});
return found;
};
}]);
我正在尝试根据函数内的条件使用 ng-class,但有时切换不起作用。我使用了 $scope.getBookmarkIcon 函数,这样即使在页面刷新后我也可以使用 bookmarkIcon class 选择 ng-class 但它不起作用 属性。
$scope.bookmarks = $cookieStore.get('data_bookmark') ? $cookieStore.get('data_bookmark') : { items:[] };
console.log($scope.bookmarks);
$scope.saveBookmark = function(resortId) {
$scope.bookmarks.items.push({ id:resortId });
$cookieStore.put('data_bookmark',$scope.bookmarks);
var cookie = $cookieStore.get('data_bookmark');
console.log($scope.bookmarked);
console.log(cookie);
};
$scope.removeBookmark = function(resortId) {
var data = $scope.bookmarks.items;
for(var key in data) {
if(data[key].id===resortId) {
var index = Object.keys(data).indexOf(key);
$scope.bookmarks.items.splice(index, 1); break;
}
}
$cookieStore.put('data_bookmark',$scope.bookmarks);
var ncookie = $cookieStore.get('data_bookmark');
console.log($scope.bookmarked);
console.log(ncookie);
};
$scope.getBookmarkIcon = function(resortId)
{
$scope.bookmarked = false;
var dataList = $scope.bookmarks.items;
dataList.filter(function(item) {
if(item.id===resortId) {
$scope.bookmarked = true;
}
});
return $scope.bookmarked;
};
}]);
这里是我到目前为止的尝试: http://plnkr.co/edit/raZeNb9FdCtfsf6FanU5?p=preview
终于解决了我的问题,分享一下。
这是完整的来源:
http://plnkr.co/edit/9NZHjAuEvC6wVN7YE8Yh?p=preview
我用一个切换功能简化了它,而不是两个功能,一个用于添加书签,另一个用于删除书签。
我初始化了 $scope.bookmarked = 错误;
然后在 html:
<td class="ellipsis" ng-model="bookmarked" ng-class="{'bookmarkIcon':getBookmarkIcon(item)}">
<a ng-click="bookmarked=!bookmarked; toggleBookmark(item);"
href="javascript:void(0);" title="{{(getBookmarkIcon(item))?'Remove
bookmark':'Bookmark this area'}}">{{item}}
</a>
</td>
然后我将 getBookmarkIcon(item) 保留在模型之外,其中仅根据 cookie 中的存储值返回 true 或 false。
var app = angular.module('myCookieBasedBookmark', ['ngCookies']);
app.controller('homeController', ['$cookieStore', '$cookies', '$scope', function($cookieStore, $cookies, $scope) {
$scope.bookmarked = false;
$scope.bookmarks = $cookieStore.get('data_bookmark') ? $cookieStore.get('data_bookmark') : { items:[] };
console.log($scope.bookmarks);
$scope.toggleBookmark = function(resortId) {
var data = $scope.bookmarks.items;
var found = false;
for(var key in data) {
if(data[key].id===resortId) {
var index = Object.keys(data).indexOf(key);
$scope.bookmarks.items.splice(index, 1); found = true; break;
}
}
if(!found){
$scope.bookmarks.items.push({ id:resortId });
}
$cookieStore.put('data_bookmark',$scope.bookmarks);
var cookie = $cookieStore.get('data_bookmark');
console.log(cookie);
};
$scope.getBookmarkIcon = function(resortId) {
var found = false;
var dataList = $scope.bookmarks.items;
dataList.filter(function(item) {
if(item.id===resortId) {
found = true;
}
});
return found;
};
}]);