【发布时间】:2018-06-09 11:22:23
【问题描述】:
我有一个 angularjs 表单正在尝试提交。范围变量的每个形式 ng-model 变量都带有点,例如 ng-model="post.title", ng-model="post.pid".
遇到的问题是,每当单击提交按钮时,post.pid 和 post.title 都会不断提醒值 undefined..
我已经梳理了stackoverflow的解决方案,我找到了这两个链接
AngularJS $scope form attribute is undefined after ng-submit
$scope with dot (.) not initializing values to input fields in angular
他们的回答是我必须首先初始化$scope.post,所以我根据两个链接提供的解决方案如下实现了它。
$scope.post = {};
$scope.submitButton = function(){
alert($scope.post.pid);
alert($scope.post.title);
}
然而,每次点击提交按钮时,它都会不断提醒未定义。
下面是完整的代码
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="post in posts">
<form>
post Id<br>
<input type='text' ng-model="post.pid" >
<br> post Title<br>
<input type='text' ng-model="post.title" name="title">
<br>
<input type='button' id='but_save' value='Save' ng-click="submitButton()">
</form>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http, $parse) {
$scope.posts = [
{
"pid" : "1",
"title" : "title1"
},
{
"pid" : "2",
"title" : "title2"
},
{
"pid" : "3",
"title" : "title3"
},
]
$scope.post = {};
$scope.submitButton = function(){
alert($scope.post.pid);
alert($scope.post.title);
}
});
</script>
</body>
</html>
【问题讨论】:
标签: angularjs