[英]ng-model is not working with ng-value in AngularJS
Below is my View page 以下是我的查看页面
<form name="form">
<label>Name</label>
<input name="name" type="text" ng-model='user.name' ng-value='emp.name' required />
<span ng-show="form.name.$touched && form.name.$invalid">Name is required</span>
<button ng-disabled="form.name.$touched && form.name.$invalid" ng-click='formUpdate()'>Update</button>
</form>
This is my controller 这是我的控制器
$scope.formUpdate = function() {
$scope.status = false;
$http({
method : 'POST',
url : 'model/update.php',
data : $scope.user ,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function mySuccess(response) {
$scope.update = response.data;
console.log(response.data);
}, function myError(response) {
$scope.update = response.statusText;
});
};
When I am using data: $scope.user
in my HTTP call I am getting blank values on console but if I used data: $scope.emp
, then I never get updated values of input fields rather getting old values of input fields. 当我在HTTP调用中使用数据: $scope.user
,我在控制台上得到的是空白值,但是如果使用数据: $scope.emp
,则将永远不会获得输入字段的更新值,而不会获得输入字段的旧值。
ng-value
binds the given expression to the value of the element. ng-value
将给定的表达式绑定到元素的值。
As I understand your question, you are trying to initialize the input value to emp.name
. 据我了解您的问题,您正在尝试将输入值初始化为emp.name
。
You should change your input to: 您应该将输入更改为:
<input type="text" ng-model='user.name' ng-init='user.name = emp.name' required />
try this code; 试试这个代码;
$scope.formUpdate = function(){
$scope.status = false;
$scope.$applyAsync();
$http({
method : 'POST',
url : 'model/update.php',
data : $scope.user ,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function mySuccess(response) {
$scope.update = response.data;
$scope.$applyAsync();
console.log(response.data);
}, function myError(response) {
$scope.update = response.statusText;
$scope.$applyAsync();
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.