Javascript 带有Cookie的Ng模型

Javascript 带有Cookie的Ng模型,javascript,angularjs,cookies,angular-ngmodel,Javascript,Angularjs,Cookies,Angular Ngmodel,我试图从angular.js主页中获取第一个示例,并添加cookie支持 这就是我到目前为止所做的: 它是: <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">

我试图从angular.js主页中获取第一个示例,并添加cookie支持

这就是我到目前为止所做的:

它是:

<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();
}]);

姓名:

你好{{mc.user}}! var myApp=angular.module('myApp',['ngCookies']); myApp.controller('MyController',[函数($cookies){ this.getCookieValue=函数(){ $cookies.put('user',this.user); 返回$cookies.get('user'); } this.user=this.getCookieValue(); }]);
但它不起作用,我一直在努力学习。
谢谢

我建议您在应用程序模块中创建这样的服务:

app.service('shareDataService', ['$cookieStore', function ($cookieStore) {

var _setAppData = function (key, data) { //userId, userName) {
    $cookieStore.put(key, data);
};

var _getAppData = function (key) {
    var appData = $cookieStore.get(key);
    return appData;
};
return {
    setAppData: _setAppData,
    getAppData: _getAppData
};
}]);
在控制器中注入shareDataService以设置和获取cookie值 作为:

工作小提琴:


我在两个控制器之间使用了cookie服务。填写文本框,看看它是如何被利用的。

好的,再次检查您的代码,下面是您的答案

  • 问题-语法错误:请注意控制器的定义,不使用[]作为第二个参数 如果您在控制器中使用[],则必须以以下方式使用它:

    myApp.controller('MyController',['$cookies',函数($cookies){ .... }]);

  • 当param
    $cookies
    将变成
    a
    b
    左右时,这种“长”格式是javascript加密安全的,并且将无法作为
    $cookies
    访问,因此您告诉控制器:“我函数中的第一个参数是
    cookies

  • 问题:您使用的是angular 1.3.x,在$cookies中没有方法PUT或GET,该方法仅在angular 1.4+中可用,因此您需要使用旧方法:
    $cookies.user='something';
    和getter:
    var something=$cookies.user;

  • 问题-您没有存储该cookie值,模型已更新,但cookie未自动绑定,因此使用$watch查看
    user
    中的更改并将其存储:

    $watch('user',函数(newValue){
    $cookies.user=newValues;
    });

  • 或者通过一些活动(单击、提交或我不知道在哪里)完成

    编辑:带有$scope的完整工作示例

    只需在代码中键入?myApp.controller('MyController'),[函数(
    $cookie
    ){应该是
    $cookies
    ?我尝试了@TamaroTamaroidny它不起作用:(我用这个链接和它的内容更新了上面的代码Hanks@Dev1你为什么推荐这种方法?我试过了,它不起作用:你能告诉我怎么用这种方法吗?如果没有这种服务方式,它将帮助我学习很多。前面的评论链接到我尝试你的方法,这是我的简单方法:你能帮我得到b吗其他的工作会教会我很多。Thaknks非常@Tamaro有可能使用getter/setter使其工作吗?我在这里尝试:谢谢你的帮助,我学到了很多$watch是$scope的方法,所以你必须使用
    $scope
    而不是
    这个
    ,也可以删除
    ng model=“mc.user”
    ,只要使用
    ng model=“user>“
    ,它在javascript中将该值绑定为
    this.mc.user
    (在angular中,您可以绑定到不存在的值/对象,它会为您创建它,”,工作示例感谢@tam我现在必须运行到床上,但会测试谢谢!
    //set
    var userData = { 'userId': $scope.userId, 'userName': $scope.userName };
    shareDataService.setAppData('userData', userData);
    
    //get
    var sharedUserData = shareDataService.getAppData('userData');
    $scope.userId = sharedUserData.userId;
    $scope.userName = sharedUserData.userName;