我想获取 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
提供双向数据绑定(bind),并在 Controller 中使用 $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>
关于javascript - 获取 Angular 的值到 localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919151/