I have a an angularjs form am trying to submit. The scope variables has each form ng-model variables with dot for example ng-model="post.title", ng-model="post.pid".
The problem that am having is that post.pid and post.title keeps on alerting values undefined whenever the submit button is clicked..
I have combed stackoverflow for solution and I found this two links
AngularJS $scope form attribute is undefined after ng-submit
$scope with dot (.) not initializing values to input fields in angular
and their answers was that I have to first initialized the $scope.post
so I have implemented it as follows based on solutions the both links provided.
$scope.post = {};
$scope.submitButton = function(){
alert($scope.post.pid);
alert($scope.post.title);
}
Yet it keeps on alerting Undefined each time the submit button is clicked.
Below is the entire code
<!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>