使用angular,我想在点击“保存”按钮时使用指令将输入值保存到cookie。 这是我的代码,我不确定我是否应该使用链接函数或控制器指令,但无论如何$ click当然在click事件处理程序中是未知的。 如何将$ cookie或$ log传递给on Click函数?
.directive('ndUserSettingCookie', ['$cookies', '$log',function() {
return {
link: function(scope, el, attrs) {
scope.savedMsg = ' items by page';
var setting = [];
scope.itemsByPage = 18;
scope.itemsByPage = (typeof setting.itemsByPage !== "undefined")? setting.itemsByPage :scope.itemsByPage;
el.on('click', function() {
var now = new Date(),
// this will set the expiration to 12 months
expireDate = new Date(now.getFullYear()+1, now.getMonth(), now.getDate());
$cookies.putObject('setting.itemsByPage', scope.itemsByPage, {'expires': expireDate});
scope.savedMsg = scope.itemsByPage + " items per page saved to your settings!";
scope.$apply();
})
}
}
}])
如果您认为我应该以另一种方式完成此操作,请告诉我。
答案 0 :(得分:0)
这是它的工作原理:
.directive('ndUserSettingCookie', ['$cookies', '$log',function($cookies, $log) {