ng-click is not sending value to the angular controller - html

I have a form and trying to send the value of the input to the angular controller but on click, the value in the controller is showing undefined.
html code
<div ng-if="questioncode == 2" class="user-form">
<div class="row">
<form>
<div class="col-sm-12">
<label class="control-label" >Phone number</label>
</div>
<div class="col-sm-12">
<input type="text" class="form-control" name="phone" placeholder="your phone number" ng-model="phone">
</div>
<div class="col-sm-12">
<input type="submit" class=" col-sm-12 btn btn-primary" ng-click="submituserPhone();" >
<flash-message duration="3000" show-close="true" on-dismiss="myCallback(flash);" ></flash-message>
</div>
</form>
</div>
</div>
angular controller
$scope.submituserPhone = function() {
console.log($scope.phone);
$http.post('insertphone',$scope.phone),then(function() {
if(response.data.status === false) {
Flash.create('danger',response.data.message);
}else{
$scope.questioncode = 3;
}
})
}

So the problem is the ng-if which is creating a new scope, hence the scope variable phone is not getting updated in the controller. Here are two methods to tackle this problem.
Using $parent:
You can use $parent to update the variable of the parent scope instead of the current one, then your variable will be visible inside the controller.
var app = angular.module('myApp', []);
app.controller('MyController', function MyController($scope) {
$scope.questioncode = 2;
$scope.submituserPhone = function() {
console.log($scope.phone);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller='MyController' ng-app="myApp">
<div ng-if="questioncode == 2" class="user-form">
<div class="row">
<form>
<div class="col-sm-12">
<label class="control-label">Phone number</label>
</div>
<div class="col-sm-12">
<input type="text" class="form-control" name="phone" placeholder="your phone number" ng-model="$parent.phone">
</div>
<div class="col-sm-12">
<input type="submit" class=" col-sm-12 btn btn-primary" ng-click="submituserPhone();">
<flash-message duration="3000" show-close="true" on-dismiss="myCallback(flash);"></flash-message>
</div>
</form>
</div>
</div>
</div>
Pass the variable as a parameter:
You can simply just pass the variable of the created scope into the controller function and update it there, as shown below.
var app = angular.module('myApp', []);
app.controller('MyController', function MyController($scope) {
$scope.questioncode = 2;
$scope.submituserPhone = function(phone) {
$scope.phone = phone;
console.log($scope.phone);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller='MyController' ng-app="myApp">
<div ng-if="questioncode == 2" class="user-form">
<div class="row">
<form>
<div class="col-sm-12">
<label class="control-label">Phone number</label>
</div>
<div class="col-sm-12">
<input type="text" class="form-control" name="phone" placeholder="your phone number" ng-model="phone">
</div>
<div class="col-sm-12">
<input type="submit" class=" col-sm-12 btn btn-primary" ng-click="submituserPhone(phone);">
<flash-message duration="3000" show-close="true" on-dismiss="myCallback(flash);"></flash-message>
</div>
</form>
</div>
</div>
</div>

Related

How to store data to browser cache directory in html?

The Project: https://github.com/marcosnunes/storeData
I need to publish into {div id="result"}{/div} the data from the form and then store to local cached directory. Would anyone help me to write this code? What I am trying to do is make a local site for to be loaded into an Android Hibrid App.
The form:
<body>
<div class="w3-container">
<div class="w3-card-4" style="width:100%">
<header class="w3-container w3-light-grey">
<h2><Div id="nameBox"></Div></h2>
</header>
<div class="w3-container">
<h2><div id="result"></div></h2>
</div>
</div>
</div>
<div class="w3-container">
<div class="row">
<div class="col-md-8">
<h3>Adicionar um Envelope e Saldo</h3>
<form name="contact-form"
method="post" id="contact-form">
<div class="form-group">
<label for="inputName">Envelope</label>
<input type="text"
class="form-control" id="cardName" name="cardName" placeholder="Envelope"
required>
</div>
<div class="form-group">
<label for="inputValue">Valor</label>
<input type="number" class="form-control" id="Value" name="Value"
placeholder="Valor" required>
</div>
<div class="form-group">
<label for="inputDescription">Descrição</label>
<input type="text" class="form-control" id="Description"
name="Description"
placeholder="Descrição" required>
</div>
<button onclick="store()" class="btn btn-primary" name="submit"
value="Submit"
id="submit_form">Adicionar</button>
</form>
</div>
</div>
</div>
</body>
</html>
I need too create new div on the button onclick()
The java code
<script>
function store(){
if (typeof(Storage) !== "undefined") {
var inputName = document.getElementById("cardName");
sessionStorage.setItem("cardName", inputName.value);
var inputValue = document.getElementById("Value");
sessionStorage.setItem("Value", inputValue.value);
var inputDescription = document.getElementById("Description");
sessionStorage.setItem("Description", inputDescription.value);
document.getElementById("result1").innerHTML = sessionStorage.getItem("cardName");
document.getElementById("result2").innerHTML = sessionStorage.getItem("Value");
document.getElementById("result3").innerHTML = sessionStorage.getItem("Description");
} else {
document.getElementById("result").innerHTML = "Ops! Alguma coisa deu errado...";
}
}
I got it !
Just inserted type="button" on button.
<button onclick="store()" **type="button"** class="btn btn-primary"
name="submit" value="Submit"
id="submit_form">Adicionar</button>

Not showing inside console window and network tab

The second part is the html code and the first part is the angularjs part .
customer.controller("new_info",
function($scope, $routeParams,$http)
{
$scope.customer = {};
$scope.register = function () {
$http.post('localhost:4000/new_info', $scope.customer).then(
function (response) {
console.log("posted successfully"); //not showing anything in the console window
}
);
}
});
<div class="col-md-6">
<div class="panel-body">
<form name="AddCustomer" >
<div class="form-group">
<label>Id</label>
<input type="text" class="form-control" ng-model="customer.id" />
</div>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" ng-model="customer.name" />
</div>
<div class="form-group">
<label>City</label>
<input type="text" class="form-control" ng-model="customer.city" />
</div>
<button class="btn btn-default" ng-click="register()">Submit</button>
</form>
</div>
</div>
//after clicking the button its not showing anything .

Angular form do not get submitted when dynamically adding row

I am trying to develop a page with a total of 6 input fields - 2 constant and 4 dynamic. A single form needs to send data with dynamic marks rows that will be added by users choice.
View :
<body ng-controller="homeController">
<div class="container">
<div class="row">
<div class="col-sm-8">
<h3>Students Form</h3>
<form name="StudentForm">
<div class="row">
<div class="col-sm-6">
<input type="text" ng-model="formData.regno" placeholder="Reg Number">
</div>
<div class="col-sm-6">
<input type="text" ng-model="formData.name" placeholder="Student Name">
</div>
</div>
<div ng-show="showMarks" ng-repeat="formData in studentMarks">
<div class="row">
<br>
<div class="col-sm-3"><input type="text" ng-model="formData.subone"
placeholder="Subject 1" style="width:70%"></div>
<div class="col-sm-3"><input type="text" ng-model="formData.subtwo"
placeholder="Subject 2" style="width:70%"></div>
<div class="col-sm-2"><input type="text" ng-model="formData.subthree"
placeholder="Subject 3" style="width:70%"></div>
<div class="col-sm-2"><input type="text" ng-model="formData.subfour"
placeholder="Subject 4" style="width:70%"></div>
<div class="col-sm-2"><button ng-click="delMarks()"> Delete</button></div>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-2">
<button ng-model="addBtnMarks" ng-click="addMarks(addBtnMarks)">
<span class="glyphicon glyphicon-plus"></span> Add
</button>
</div>
<div class="col-sm-2">
<button ng-click="editEnquiry()"> Edit</button>
</div>
<div class="col-sm-2">
<button type="submit" ng-click="saveFullForm()"> Save</button>
</div>
<div class="col-sm-2">
<button onclick="window.print()"> Print</button>
</div>
<div class="col-sm-2"></div>
</div>
</form>
</div>
<div class="col-sm-4"></div>
</div>
<br>
<br>
<br>
</div>
</body>
Controller :
var std = angular.module("studentsApp",[]);
std.controller("homeController", function($scope){
$scope.showMarks=false;
$scope.studentMarks=[];
$scope.addMarks = function (){
$scope.showMarks=true;
var rowConut = $scope.studentMarks.length +1;
$scope.studentMarks.push({
subone:0,
subtwo:0,
subthree:0,
subfour:0,
});
console.log(rowConut);
console.log($scope.studentMarks);
};
$scope.delMarks = function (){
rowConut = $scope.studentMarks.length -1;
$scope.studentMarks.pop();
console.log(rowConut);
console.log($scope.studentMarks);
};
$scope.saveFullForm = function(){
//
console.log($scope.formData);
console.log($scope.marks);
console.log($scope.studentMarks);
};
});
Problem is:
When I click -save button form data for dynamic rows does not get shown up
in the console. Also, when adding more than one row, -delete button does
not deletes individual rows. The last row gets deleted by default. What might be the
problem in the code.
When using array.pop, it will remove the last element in the array. The solution is to pass the element index from the view through ng-click and splice the array based on the index , thus the element will be remove out from the array
View
<div class="col-sm-2"><button ng-click="delMarks($index)"> Delete</button></div>
Controller
$scope.delMarks = function (index){
rowConut = $scope.studentMarks.length -1;
$scope.studentMarks.splice(index,1);
console.log(rowConut);
console.log($scope.studentMarks);
};
Update
var std = angular.module("studentsApp",[]);
std.controller("homeController", function($scope){
$scope.showMarks=false;
$scope.studentMarks=[];
$scope.addMarks = function (){
$scope.showMarks=true;
var rowConut = $scope.studentMarks.length +1;
$scope.studentMarks.push({
subone:$scope.formData.regno,
subtwo:0,
subthree:0,
subfour:0,
});
//console.log(rowConut);
//console.log($scope.studentMarks);
};
$scope.delMarks = function (index){
rowConut = $scope.studentMarks.length -1;
$scope.studentMarks.splice(index,1);
//$scope.studentMarks.pop();
//console.log(rowConut);
//console.log($scope.studentMarks);
};
$scope.saveFullForm = function(){
//
console.log($scope.formData);
console.log($scope.marks);
console.log($scope.studentMarks);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="studentsApp">
<body ng-controller="homeController">
<div class="container">
<div class="row">
<div class="col-sm-8">
<h3>Students Form</h3>
<form name="StudentForm">
<div class="row">
<div class="col-sm-6">
<input type="text" ng-model="formData.regno" placeholder="Reg Number">
</div>
<div class="col-sm-6">
<input type="text" ng-model="formData.name" placeholder="Student Name">
</div>
</div>
<div ng-show="showMarks" ng-repeat="formData in studentMarks">
<div class="row">
<br>
<div class="col-sm-3"><input type="text" ng-model="formData.subone"
placeholder="Subject 1" style="width:70%"></div>
<div class="col-sm-3"><input type="text" ng-model="formData.subtwo"
placeholder="Subject 2" style="width:70%"></div>
<div class="col-sm-2"><input type="text" ng-model="formData.subthree"
placeholder="Subject 3" style="width:70%"></div>
<div class="col-sm-2"><input type="text" ng-model="formData.subfour"
placeholder="Subject 4" style="width:70%"></div>
<div class="col-sm-2"><button ng-click="delMarks($index)"> Delete</button></div>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-2">
<button ng-model="addBtnMarks" ng-click="addMarks()">
<span class="glyphicon glyphicon-plus"></span> Add
</button>
</div>
<div class="col-sm-2">
<button ng-click="editEnquiry()"> Edit</button>
</div>
<div class="col-sm-2">
<button type="submit" ng-click="saveFullForm()"> Save</button>
</div>
<div class="col-sm-2">
<button onclick="window.print()"> Print</button>
</div>
<div class="col-sm-2"></div>
</div>
</form>
</div>
<div class="col-sm-4"></div>
</div>
<br>
<br>
<br>
</div>
</body>
</html>

ng-click not fire function in controller

The bug is very weird, I have been stucking here for hours just can find why my function in controller is fired.
Here is my HTML code:
<div class="col-md-9 clearfix" id="customer-account" ng-controller='ProfileController'>
<div class="box clearfix">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_old">oldpassword</label>
<input type="password" class="form-control" id="password_old" ng-model="passwordold">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="password_1">newpassword</label>
<input type="password" class="form-control" id="password_1" ng-model="passwordnew1">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="password_2">retype</label>
<input type="password" class="form-control" id="password_2"ng-model="passwordnew2">
</div>
</div>
<p>{{passwordnew2}}</p>
</div>
<!-- /.row -->
<div class="text-center">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save" ng-click="changePwd(passwordold,passwordnew1,passwordnew2)"></i> save
</button>
</div>
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>
</div>
</div>
After I click the Button, which ng-click attribute as you can see, nothing happen.
Here is my controller code:
controller('ProfileController', ['$scope','UserFactory','SharedDataFactory',
function($scope,UserFactory,SharedDataFactory){
$scope.user = UserFactory.user;
$scope.passwordold;
$scope.errorMessageChangepwd='error';
$scope.showErrMsgChangepwd = false;
$scope.passwordnew1;
$scope.passwordnew2;
$scope.changePwd = function(passwordold,passwordnew1,passwordnew2){
console.log("aaaaaaaaaa");
if (passwordnew1!==passwordnew2){
$scope.showErrMsgChangepwd= true;
$scope.errorMessageChangepwd = 'error';
}else{
UserFactory.changePwd(passwordnew1)
.catch(function(err){
console.log(err.data);
}).then(function(response){
console.log(response);
});
}
};}]);
I called console.log("aaaaaaaaaa"); in the first line of my function, but after I click the button, nothing is shown on console.
And also
<div class="text-center" >
<p>{{errorMessageChangepwd}}aaaa</p>
</div>`
does not show error aaaa as expected but aaa on the browser.
what could be wrong?
Thanks.
You need to add ng-controller="ProfileController" in the top of your form.
Which looks like missing on your code.

ng-repeat not displaying data or repeating in Angular

I am new to Angular and I am trying to build a search page, I have a button that executes a function to get data from the server when it is clicked. The data is returned to the client, now I want to be able to display the data on that same page.
I've tried something but it does not show. What am I doing wrong?
This is my html:
<div class="search-area col-md-12 no-padding" ng-controller="SearchController as search">
<div class="col-md-3">
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-12">
<select id="From" name="From" ng-model="d.from" class="form-control"
ng-options="item.id as item.dest_name for item in d.destinations">
</select>
</div>
</div>
</div>
<div class="col-md-3">
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-12">
<select id="To" name="To" ng-model="d.to" class="form-control"
ng-options="item.id as item.dest_name for item in d.destinations">
</select>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<div class="col-md-12">
<input id="When" name="When" ng-model="d.when" placeholder="When" class="form-control input-md" required="" data-provide="datepicker" data-date-format="mm/dd/yyyy">
</div>
</div>
</div>
<div class="col-md-3">
<a ng-click="getSchedules()" class="submit">Buy Ticket</a>
</div>
</div>
<div class="clear"></div>
<div class="col-md-12" ng-repeat="schedule in d.schedules">
<div class="form-group">
<div class="col-md-12" ng-show="schedule.length">
<% schedule.transport_entity %>
</div>
</div>
</div>
search.js
(function() {
var app = angular.module('search', []);
app.controller('SearchController', ['$http', '$scope', function($http, $scope) {
var search = this;
this.destinations = [];
$scope.d = {};
$scope.getSchedules = function() {
$http.get('/trips/schedules?from='+$scope.d.from+'&to='+$scope.d.to+'&when='+$scope.d.when).success(function(data) {
$scope.d.schedules = data; // Getting The Search Results Here
});
};
$http.get('/trips/destinations').success(function(data) {
$scope.d.destinations = data;
});
}]);
}) ();
Store your data in the $scope rather than in properties of the controller itself. Also you can simplify your logic considerably by making proper use of data binding - if you set an ng-model on the form controls and use ng-options for the select boxes then you don't need any of the jQuery logic to extract values from the fields and generate the option elements - angular will do it all for you.
<div class="search-area col-md-12 no-padding" ng-controller="SearchController as search">
<div class="col-md-3">
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-12">
<select id="From" name="From" ng-model="data.from" class="form-control" ng-options="item.id as item.dest_name for item in data.destinations">
</select>
</div>
</div>
</div>
<div class="col-md-3">
<!-- Select Basic -->
<div class="form-group">
<div class="col-md-12">
<select id="To" name="To" ng-model="data.to" class="form-control" ng-options="item.id as item.dest_name for item in data.destinations">
</select>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<div class="col-md-12">
<input id="When" name="When" ng-model="data.when" placeholder="When" class="form-control input-md" required="" data-provide="datepicker" data-date-format="mm/dd/yyyy">
</div>
</div>
</div>
<div class="col-md-3">
<a ng-click="getSchedules()" class="submit">Buy Ticket</a>
</div>
</div>
<div class="clear"></div>
<div class="col-md-12" ng-repeat="schedule in data.schedules">
<div class="form-group">
<div class="col-md-12" ng-show="schedule.length">
<% schedule.transport_entity %>
</div>
</div>
</div>
search.js
(function() {
var app = angular.module('search', []);
app.controller('SearchController', ['$http', '$scope' function($http, $scope) {
$scope.data = {};
$scope.getSchedules = function() {
$http.get('/trips/schedules?from='+$scope.data.from+'&to='+$scope.data.to+'&when='+$scope.data.when)
.success(function(data) {
$scope.data.schedules = data; // Getting The Search Results Here
});
};
$http.get('/trips/destinations').success(function(data) {
$scope.data.destinations = data;
});
}]);
}) ();
You need to add it to the scope.
$scope.search = this;
And when storing the results of the search..
$scope.search.schedules = data;
Then it will be available in the view.