gpt4 book ai didi

javascript - 更新范围的 Angular 方式是什么?

转载 作者:行者123 更新时间:2023-12-03 11:28:08 26 4
gpt4 key购买 nike

我有一个简单的双向绑定(bind)设置,并添加了一个简单的 JavaScript 函数,该函数根据用户点击的图像的“alt”标签更新值。问题是,当我点击“保存”时,数据不会更新,直到我在文本框内单击并添加空格。解决这个问题的最佳方法是什么?

http://plnkr.co/edit/j6tPYYUqvRyvfs32mcrW?p=preview

    angular.module('copyExample', [])
.controller('ExampleController', ['$scope',
function($scope) {
$scope.master = {};

$scope.update = function(user) {
// Example with 1 argument
$scope.master = angular.copy(user);
};

$scope.reset = function() {
// Example with 2 arguments
angular.copy($scope.master, $scope.user);
};

$scope.reset();
}
]);
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>

<body ng-app="copyExample">
<div ng-controller="ExampleController">
<form novalidate class="simple-form">
Name:
<input type="text" ng-model="user.name" id="name" />
<br />
<img src="http://placehold.it/100&text=John" onclick="changeText(alt)" alt="John" />
<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>
function changeText(value) {
document.getElementById('name').value = value
}
</script>
</body>

</html>

最佳答案

工作 fiddle - http://jsfiddle.net/lwalden/b2p7pu7g/

如果您已经在使用 Angular,那么不要为图像点击事件编写普通的 JavaScript 函数 - 使用 Angular。

此外,建议不要以这种方式使用 alt 属性。您可以改用 data- 属性。

Additional attributes within a tag to pass values to a function

关于javascript - 更新范围的 Angular 方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26836484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com