Javascript 表单在AngularJS中的视图和服务器上不更新?
我的web应用程序中有此错误。所以,我有一个表单,当我编辑表单时,它不会在视图和服务器上更新。我想解决的是,当我编辑表单时,我想更新视图和服务器。下面是我的代码。请检查我的代码,如果有什么错误。提前谢谢。有什么帮助吗 这是我的密码 关于 家庭控制器Javascript 表单在AngularJS中的视图和服务器上不更新?,javascript,angularjs,api,put,Javascript,Angularjs,Api,Put,我的web应用程序中有此错误。所以,我有一个表单,当我编辑表单时,它不会在视图和服务器上更新。我想解决的是,当我编辑表单时,我想更新视图和服务器。下面是我的代码。请检查我的代码,如果有什么错误。提前谢谢。有什么帮助吗 这是我的密码 关于 家庭控制器 这里有几个问题 来自服务器的数据未绑定到控制器,这意味着它不会显示在您的视图中 表单的输入绑定到别名,而不是实际作用域的任何成员 提交数据更改后,需要从服务器获取更新 保存数据后,inactive范围值不会更新 这意味着您的“保存”按钮在提交表单后
这里有几个问题
- 来自服务器的数据未绑定到控制器,这意味着它不会显示在您的视图中
- 表单的输入绑定到别名,而不是实际作用域的任何成员
- 提交数据更改后,需要从服务器获取更新
- 保存数据后,
范围值不会更新 这意味着您的“保存”按钮在提交表单后不会隐藏inactive
MyCtrl
控制器中填充$scope.userInfo
。在HomeController
中设置$scope.userInfo
时,只有HomeController
可以访问$scope.userInfo
,而不是MyCtrl
您需要更改代码,以便MyCtrl
将$scope.userInfo
设置在它自己的作用域中,以便它可以访问以下数据:
app.controller('MyCtrl', function($scope, $window, people) {
// This function will now be called so that it can get the userInfo
// from the server and populate into the scope and give the controller
// access.
people.getUserInfo().then(function (response) {
$scope.userInfo = response.data;
});
// ...
});
现在,您需要实际将表单的输入绑定到范围中的数据
<form>
<fieldset ng-disabled="inactive">
<legend>Info</legend>
<b>First Name:</b>
<input type="text" ng-model="userInfo.lawyers[$index].firstName">
<br>
<b>Last Name:</b>
<input type="text" ng-model="userInfo.lawyers[$index].lastName">
<br>
<b>Email:</b>
<input type="email" ng-model="userInfo.lawyers[$index].email">
<br>
<b>Website:</b>
<input type="text" ng-model="userInfo.lawyers[$index].website">
<br>
</fieldset>
</form>
最后,对于Save
按钮问题,您需要确保作用域的isactive
值设置适当,以适应您不希望它出现在DOM中的情况。将该值设置为true
会隐藏该值,而设置为false
会显示该值。“编辑”按钮已为您切换此值,但您也可以在希望其执行的函数完成时切换此值。您可以在updateUser
功能中执行此操作:
app.controller('MyCtrl', function($scope, $window, people) {
// ...
$scope.updateUser = function(person) {
// Hide the save button
$scope.inactive = true;
people.updateUser(person)
.then(function() {
return people.getUserInfo();
}).then(function (response) {
$scope.userInfo = response.data;
});
};
// ...
});
可以找到AngularJS作用域的其他资源。当您在$scope.userInfo.layers
函数之外设置$scope.confirmedAction
值时,是否会发生任何情况?获取错误:无法读取未定义的属性“layers”
如何定义$scope.userInfo
?您需要将其设置在该函数之外,否则在调用该函数之前它将保持未定义。@Mike在我更新的问题中显示的myHomeController
中,我在控制台中遇到一个错误:SyntaxError:missing)在参数列表之后
并且应用程序未定义
在我做的第一个括号之后:)代码>仍然不起作用。@SuzyHakobyan更新后将app
定义放回文件顶部,并修复了语法问题。在问题中有一个额外的})需要删除的App.js
文件底部的code>。我没有任何更新。单击“保存”时,“保存”按钮不会返回到“编辑禁用”模式
var isConfirmed = false;
app.controller('HomeController', function($scope, people) {
if (!isConfirmed) {
people.getUserInfo().then(function (response) {
$scope.userInfo = response.data;
}, function (error) {
console.log(error)
});
}
});
app.controller('MyCtrl', function($scope, $window, people) {
// This function will now be called so that it can get the userInfo
// from the server and populate into the scope and give the controller
// access.
people.getUserInfo().then(function (response) {
$scope.userInfo = response.data;
});
// ...
});
<form>
<fieldset ng-disabled="inactive">
<legend>Info</legend>
<b>First Name:</b>
<input type="text" ng-model="userInfo.lawyers[$index].firstName">
<br>
<b>Last Name:</b>
<input type="text" ng-model="userInfo.lawyers[$index].lastName">
<br>
<b>Email:</b>
<input type="email" ng-model="userInfo.lawyers[$index].email">
<br>
<b>Website:</b>
<input type="text" ng-model="userInfo.lawyers[$index].website">
<br>
</fieldset>
</form>
app.controller('MyCtrl', function($scope, $window, people) {
// ...
$scope.updateUser = function(person) {
people.updateUser(person)
// Now get the new data.
.then(function() {
return people.getUserInfo();
}).then(function (response) {
// Apply the new data to the scope.
$scope.userInfo = response.data;
});
};
// ...
});
app.controller('MyCtrl', function($scope, $window, people) {
// ...
$scope.updateUser = function(person) {
// Hide the save button
$scope.inactive = true;
people.updateUser(person)
.then(function() {
return people.getUserInfo();
}).then(function (response) {
$scope.userInfo = response.data;
});
};
// ...
});