我想從angular.js主頁採取第一個例子並添加cookie支持。Ng模型與Cookie
這是我到目前爲止有:https://jsfiddle.net/y7dxa6n8/8/
是:
<div ng-app="myApp">
<div ng-controller="MyController as mc">
<label>Name:</label>
<input type="text" ng-model="mc.user" placeholder="Enter a name here">
<hr>
<h1>Hello {{mc.user}}!</h1>
</div>
</div>
var myApp = angular.module('myApp', ['ngCookies']);
myApp.controller('MyController', [function($cookies) {
this.getCookieValue = function() {
$cookies.put('user', this.user);
return $cookies.get('user');
}
this.user = this.getCookieValue();
}]);
但它不工作,我一直在努力學習的角度。 感謝
只是在你的代碼中輸入錯誤? myApp.controller('MyController',[函數('$ cookie'){應該是'$ cookies'?) –
我試過@TamaroTamaroidny它沒有工作:(https:// jsfiddle。net/y7dxa6n8/8/i更新了以上鍊接及其內容代碼 – Noitidart