试着理解角,收到
Trying to understand angular.copy
本文关键字:收到 更新时间:2023-09-26
我试图理解什么角。复制就可以了。我在Angular js文档中见过一个例子,但无法弄清楚为什么重置按钮在填满所有文本字段并点击保存按钮后不起作用。但如果我们在点击保存之前点击重置按钮,它就会起作用。有人能解释一下吗?提前感谢
index . html:
<div ng-controller="ExampleController">
<form novalidate class="simple-form">
<label>Name: <input type="text" ng-model="user.name" /></label><br />
<label>Age: <input type="number" ng-model="user.age" /></label><br />
Gender: <label><input type="radio" ng-model="user.gender" value="male" />male</label>
<label><input type="radio" ng-model="user.gender" value="female" />female</label><br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
</form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div>
script.js:
angular.
module('copyExample', []).
controller('ExampleController', ['$scope', function($scope) {
$scope.master = {};
$scope.reset = function() {
// Example with 1 argument
$scope.user = angular.copy($scope.master);
};
$scope.update = function(user) {
// Example with 2 arguments
angular.copy(user, $scope.master);
};
$scope.reset();
}]);
在您点击重置之前,$scope.user
甚至不存在。
Angular
copy
方法仅为from分配另一个内存实例master
到user
。如果您不想在不更改master
的情况下更改user
,则可以避免引用问题
引用在这里
你的问题是在$scope.user
而不仅仅是user
:
工作片段如下:
$scope.update = function(user) {
// Example with 2 arguments
angular.copy($scope.user, $scope.master);
};
(function(angular) {
'use strict';
angular.module('includeExample', ['ngAnimate'])
.controller('ExampleController', ['$scope', '$q',
function($scope, $q) {
$scope.master = {};
$scope.reset = function() {
// Example with 1 argument
$scope.user = angular.copy($scope.master);
};
$scope.update = function(user) {
// Example with 2 arguments
angular.copy($scope.user, $scope.master);
};
$scope.reset();
}
]);
})(window.angular);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular-animate.js"></script>
<body ng-app="includeExample">
<div ng-controller="ExampleController">
<form novalidate class="simple-form">
<label>Name: <input type="text" ng-model="user.name" /></label><br />
<label>Age: <input type="number" ng-model="user.age" /></label><br />
Gender: <label><input type="radio" ng-model="user.gender" value="male" />male</label>
<label><input type="radio" ng-model="user.gender" value="female" />female</label><br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
</form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div>
</body>
相关文章:
- gulp Iconify任务抛出错误TypeError:Path必须是字符串.收到false
- 如何在收到电子邮件时自动调用脚本
- JShint-.jshintrc中的ES6有esversion,但仍收到警告(使用atom)
- 大气:服务器收到消息
- 为什么从数组调用时收到非法调用
- 一旦接收到iFrame的响应,就捕获该响应
- nodeJS:使用(fs)尝试访问静态资源,但收到404错误
- 我收到此错误Uncatch TypeError:this.getElements不是一个函数
- 我一直收到的控制台警告是什么?推迟长时间运行的计时器任务以提高滚动的流畅性
- 我收到一个类型错误,其中包含-"$不是函数“;使用noConflict时
- 为什么我在简单的 AngularJS 程序中收到此错误
- 为什么当订阅两次rx 2.3中的observable时,第二次订阅没有收到任何值
- Fine Uploader-未收到来自已加载iframe的有效消息
- 收到“范围错误: 超出最大调用堆栈大小”错误
- 无法查看内容,因为尚未收到数据
- 我收到消息“资源解释为脚本,但使用 MIME 类型文本/html 传输”
- 指令中未收到广播
- 收到错误时如何防止在提交()上重定向
- 为什么在应用程序打开时收到通知时弹出UIAlertview(科尔多瓦)
- 从 postmaster@domain.co.uk 收到的 PHP 电子邮件表单