Angularjs Ng init在通过Ng repeat时显示相同的检索表单值
我对angularjs ng-init()有问题。在名为post的表中,插入了3行。比如说Angularjs Ng init在通过Ng repeat时显示相同的检索表单值,angularjs,Angularjs,我对angularjs ng-init()有问题。在名为post的表中,插入了3行。比如说 insert into post(id,title)values(1,'title1'); insert into post(id,title)values(2,'title2'); insert into post(id,title)values(3,'title3'); 现在我已经检索了记录并通过ng repeat传递,一切正常 我的问题是ng-init()函数。当我用ng repeat中初始化的n
insert into post(id,title)values(1,'title1');
insert into post(id,title)values(2,'title2');
insert into post(id,title)values(3,'title3');
现在我已经检索了记录并通过ng repeat传递,一切正常
我的问题是ng-init()函数。当我用ng repeat中初始化的ng init将数据库值传递到表单inputs时,它会显示表单inputs中的最后一个数据库值(例如3,标题3)
您的代码中几乎没有问题:您的问题中有一些问题
不需要绑定数据$parent
- 您在每个输入中使用了两次ng模型,这是错误的
- 此外,ng模型自动绑定数据,不需要ng init
帖子Id
帖子标题
var-app=angular.module(“myApp”,[]);
app.controller(“myCtrl”,函数($scope){
$scope.posts=[
{
“pid”:“1”,
“标题”:“标题1”
},
{
“pid”:“2”,
“标题”:“标题2”
},
{
“pid”:“3”,
“标题”:“标题3”
},
]
$scope.submitButton=函数(){
警报(angular.toJson($scope.posts))
}
});
您的代码中几乎没有问题:您的问题中有一些问题
不需要绑定数据$parent
- 您在每个输入中使用了两次ng模型,这是错误的
- 此外,ng模型自动绑定数据,不需要ng init
帖子Id
帖子标题
var-app=angular.module(“myApp”,[]);
app.controller(“myCtrl”,函数($scope){
$scope.posts=[
{
“pid”:“1”,
“标题”:“标题1”
},
{
“pid”:“2”,
“标题”:“标题2”
},
{
“pid”:“3”,
“标题”:“标题3”
},
]
$scope.submitButton=函数(){
警报(angular.toJson($scope.posts))
}
});
为什么选择$parent?
您可以直接绑定post.id
@Sravan,如果不使用$parent,在您尝试提交表单时,它会将这些表单值显示为未定义。您可以选择范围对象变量instead@chinazaike为什么首先需要ng init
?您可以直接使用ng model=“post.id”
和ng model=“post.title”
。我想,我不明白整个用例。你介意解释一下吗?@Pankaj如果使用例如ng modl=“post.title”。现在如何将其作为表单变量传递。例如,我尝试了var pid=$scope.post.pid;或var pid=$scope.pid;警报(pid);但是它不再起作用了为什么你选择了$parent?
你可以直接绑定post.id
@Sravan,如果没有$parent,当你尝试提交表单时,它会将那些表单值显示为未定义。你可以选择范围对象变量instead@chinazaike为什么首先需要ng init
?您可以直接使用ng model=“post.id”
和ng model=“post.title”
。我想,我不明白整个用例。你介意解释一下吗?@Pankaj如果使用例如ng modl=“post.title”。现在如何将其作为表单变量传递。例如,我尝试了var pid=$scope.post.pid;或var pid=$scope.pid;警报(pid);但是它不再工作了,谢谢你的解决方案,但是我不能再将ng模型值作为表单变量传递,这样我就可以提交每个数据了。例如,我尝试了var pid=$scope.post.pid;或var pid=$scope.pid;警报(pid);但是它不再起作用了,是吗?你能再多用一点吗?谢谢你的解决方案,但我不能再把ng模型值作为表单变量传递,这样我就可以提交每个数据了。例如,我尝试了var pid=$scope.post.pid;或var pid=$scope.pid;警报(pid);但是它不再起作用了,是吗?你能瞄准更多吗?
<input type='text' ng-model='title' ng-model='$parent.title' ng-init="$parent.title=post.title" >
<!doctype html>
<html>
<head>
</head>
<body ng-app='myapp'>
<div class="content" ng-controller='fetchCtrl'>
<div class="post" ng-repeat='post in posts'>
<form bind-to-scope='$parent' >
post Id<br>
<input type='text' ng-model='pid' ng-model='$parent.pid' ng-init="$parent.pid=post.id" >
<br>
post Title<br>
<input type='text' ng-model='title' ng-model='$parent.title' ng-init="$parent.title=post.title" >
<br>
<input type='button' id='but_save' value='Save' ng-click="submitButton()" >
</form>
</div>
</div>
</div>
<!-- Script -->
<script src="angular.min.js"></script>
<script>
var fetch = angular.module('myapp', []);
fetch.controller('userCtrl', ['$scope', '$http', function ($scope, $http) {
// Add new record
$scope.submitButton = function(){
var pid=$scope.pid;
//alert variables values that is all I want
alert(pid);
// do http thing here if you like
$http({
});
}
// Fetch post data scope goes here
</script>
</body>
</html>