angularを使用して、保存ボタンをクリックしたときにディレクティブを使用して入力の値をCookieに保存したいと思います。これが私のコードです。リンク関数またはコントローラーディレクティブを使用する必要があるかどうかはわかりませんでしたが、とにかくクリックイベントハンドラーでは $cookie はもちろん不明です。$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();
})
}
}
}])
これをまったく別の方法で行う必要があると思われる場合は、お知らせください。