【问题标题】:Get value of angular to localStorage获取 angular 到 localStorage 的值
【发布时间】:2019-11-17 00:54:38
【问题描述】:

我想获取 angular 的值并将其存储在我的本地存储中。像这样:

<input type="text" id="ID" value="{{sudent.id}}">

<script>
  localStorage.setItem("STUDID", document.getElementById("ID").value);
</script>

我希望结果是:

<script>
  localStorage.setItem("STUDID", "HUMMS-201906");
</script>

【问题讨论】:

  • ng-model 用于input 元素并在控制器中使用$scope 访问输入值。
  • ngModel 指令通过将模型同步到视图以及视图到模型来提供双向数据绑定。阅读AngularJS Developer Guide - forms

标签: javascript angularjs local-storage


【解决方案1】:

使用ng-model提供双向数据绑定,并在控制器中使用$scope访问ng-model值。

angular.module('inputExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.student = {
      "id": 101
    };

    console.log($scope.student.id);
  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<body ng-app="inputExample">
  <div ng-controller="ExampleController">
    <input type="text" name="studentId" ng-model="student.id" required>
  </div>
</body>

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 2019-03-28
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2021-06-06
    • 2014-06-15
    相关资源
    最近更新 更多