AngularJs - get value from ng-model (ng-option) - angularjs

I'm creating a dependency dropdown from JSON(country, city). Based on country selection city will populate in second dropdown. If I get the value of country it return object with city list.
please wait to get country dropdown value from github raw.
var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
$scope.istrue = true;
$scope.userInfo = [];
$http.get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json').then(function(response){
$scope.countriesToCities = response.data;
//console.log($scope.countriesToCities)
},function(response){
console.log(response.status);
});
$scope.populateCities = function() {
$scope.istrue = false;
//console.log($scope);
$scope.cityArray = $scope.users.country;
}
$scope.selectedCity = function(){
console.log($scope.city)
}
$scope.pushInArray = function() {
var user = angular.copy($scope.users);
$scope.userInfo.push(user);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">
<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="email" ng-model="users.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
<select ng-model="users.country" ng-change='populateCities()' ng-options="x for (x, y) in countriesToCities"></select>
<select ng-hide="istrue" ng-model="users.city" ng-change='selectedCity()' ng-options="x for x in cityArray"></select>
<button ng-click="pushInArray()">Add</button>
<pre>{{userInfo}}</pre>
</div>
How to get the value of country?

Working snippet with modified $scope.populateCities(), $scope.user = {} object and ng-options to select correct country.
var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
$scope.istrue = true;
$scope.userInfo = [];
$scope.user = {};
$http.get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json').then(function(response){
$scope.countriesToCities = response.data;
//console.log($scope.countriesToCities)
},function(response){
console.log(response.status);
});
$scope.populateCities = function() {
$scope.istrue = false;
angular.forEach($scope.countriesToCities, function(values, key) {
if($scope.user.country == key) {
$scope.cityArray = values;
}
});
}
$scope.pushInArray = function() {
var user = angular.copy($scope.user);
$scope.userInfo.push(user);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">
<input type="text" name="name" ng-model="users.name" placeholder="Name">
<input type="text" name="name" ng-model="user.name" placeholder="Name">
<input type="text" name="email" ng-model="user.email" placeholder="Email">
<input type="text" name="phoneNo" ng-model="user.phoneNo" placeholder="phone Number">
<select ng-model="user.country" ng-change='populateCities()' ng-options="country as country for (country, cities) in countriesToCities"></select>
<select ng-hide="istrue" ng-model="user.city" ng-change='selectedCity()' ng-options="x for x in cityArray"></select>
<button ng-click="pushInArray()">Add</button>
<pre>{{userInfo}}</pre>
</div>

Related

By default manual one checkbox checked , store 0,1 store based on check box checked, check box checked based on retriving '0','1'

<input type="text" class="form-control" placeholder="Service Number" id="service_num" name="service_num" ng-model="service_num" ng-blur="GetUfdreportdata()">
<span>Details</span>
<label>
<input type="checkbox" class="detailcheckbok" id="deatilsmanual_0" name="deatailsmanual" ng-model="deatilsmanual_0" value="0" checked/> Manual </label>
<label>
<input type="checkbox" class="detailcheckbok" id="deatilsmanual_1" name="deatailsmanual" ng-model="deatilsmanual_1" value="1" /> Auto </label>
<input type="button" value="Submit" ng-click="myfunction()" />
app.controller("cap", function($scope, $http, $mdDialog) {
$scope.myfunction = function() {
$http.post("save_report.php", {
}
}
$scope.GetUfdreportdata = function() {
$http({
method: "GET",
url: "api/api.php",
params: {
service_num: $scope.service_num
}
}).success(function(data) {}
});
Try the following code to store the 0,1 values based on checkbox behavior.
<script>
angular.module('checkboxExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.checkboxModel = { value1 : 1, value2 : 1 };
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
<label>Value1:
<input type="checkbox" ng-model="checkboxModel.value1"
ng-true-value="1" ng-false-value="0">
</label><br/>
<label>Value2:
<input type="checkbox" ng-model="checkboxModel.value2"
ng-true-value="1" ng-false-value="0">
</label><br/>
<tt>value1 = {{checkboxModel.value1}}</tt><br/>
<tt>value2 = {{checkboxModel.value2}}</tt><br/>
</form>

Need to filter model based on make in angularjs. below code is not working model comes empty if filtered on make

Need to Filter options in dropdown based on manufacturers (make). Need to filter models based on selected make option via "ng-model:make"
HTML:
<div class="form-group">
<label for="make">Make</label>
<select class="form-control" data-ng-model="make" data-ng-options="maker.make for maker in makers" required></select>
</div>
<div class="form-group">
<label for="make">Make</label>
<select class="form-control" data-ng-model="make" data-ng-options="maker.make for maker in makers" required></select>
</div>
<div class="form-group">
<label for="model">Model</label>
<select class="form-control" data-ng-model="model" data-ng-options="availabledevice.model for availabledevice in availabledevices|filter:make" required></select>
</div>
Services from factory:
deviceassign.factory("manufacturersoptionsservice", function($http) {
var devicemakers = {};
devicemakers.values = function() {
return $http.get('api/displaymanufacturersapi.php');
}
return devicemakers;
});
deviceassign.factory("availabledeviceservice", function($http) {
var availabledevice = {};
availabledevice.details = function() {
return $http.get('api/displayavailabledevicesforoptionsapi.php');
}
return availabledevice;
});
Controller:
deviceassign.controller('assigndevicecontroller', function($scope, growl, manufacturersoptionsservice, availabledeviceservice) {
manufacturersoptionsservice.values().success(function(response1) {
$scope.makers = response1;
});
availabledeviceservice.details().success(function(response3) {
$scope.availabledevices = response3;
});
// ...
});
Its working now.Had to select make.make as make itself is an object in JSON
<select class="form-control" data-ng-model="model" data-ng-options="availabledevice.model for availabledevice in availabledevices|filter:make.make" required></select>

angularjs with ng-repeat. cant access the data in controler when i use ng-repeat

I use the following ng-model with ng-repeat and I tried to access the data in my controller but it seems that the data cant be accessed there.
<div ng-controller="myCtrl" class="container">
<fieldset data-ng-repeat="contact in choices">
<input class="form-control" ng-model='contact.firstname'>
<input class="form-control" ng-model='contact.lastname'>
<input class="form-control" ng-model='contact.email'>
<input class="form-control" ng-model='contact.contact'>
<input class="form-control" ng-model='contact.adress'>
<input class="form-control" ng-model='contact.city'>
<input class="form-control" ng-model='contact.state'>
<button class="btn btn-primary" ng-click="addcontact()">Add</button></td>
</fieldset>
<button class="addfields" ng-click="addNewChoice()">Add fields</button>
<div id="choicesDisplay">
{{ choices }}
</div>
</div>
</div>
</div>
And use the following ng-app
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.choices = [{
id: 'choice1'
}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length + 1;
$scope.choices.push({
'id': 'choice' + newItemNo
});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length - 1;
$scope.choices.splice(lastItem);
};
var refresh = function() {
$http.get('/contactlist').success(function(response) {
console.log("in m new controler now");
$scope.contactlist = response;
$scope.contact = "";
});
};
refresh();
$scope.addcontact = function() {
console.log($scope.contact);
$http.post('/contactlist', $scope.contact).success(function(response)
{
console.log(response);
refresh();
});
};
});
I can't access the data in $scope for the dynamically added controls through ng-repeat
In order to acess the data you should use
$scope.choices[index]
Index is the number of index you want to acces

how to submit form value which have same name in angular js?

I have a multiple for filed which have a same name in array formate like
<input type='text' name="frm[]">. I want to submit this in AngularJS and get value in PHP.
my script:
<input type="text" name="textval" id="textval1" ng-model="data.textvalt[1]" >
<input type="text" name="textval" id="textval2"ng-model="data.textvalt[2]">
<input type="text" name="textval" id="textval3" ng-model="data.textvalt[3]">
Use Angular's $http service to post the data to your php file.
Check the below code
<html ng-app="submitExample">
<script type="text/javascript" src="lib\angular.js"></script>
<script type="text/javascript" src="index.js"></script>
<form ng-submit="submit()" ng-controller="FormController">
Enter text and hit enter:
<input type="text" name="textval" id="textval1" ng-model="data.textvalt[1]" >
<input type="text" name="textval" id="textval2"ng-model="data.textvalt[2]">
<input type="text" name="textval" id="textval3" ng-model="data.textvalt[3]">
<input type="submit" id="submit" value="Submit" />
</form>
<html>
var app = angular.module('submitExample', [])
app.controller('FormController', ['$scope','$http', function($scope,$http) {
$scope.data = {};
$scope.data.textvalt = new Array();
$scope.data.textvalt[1] = '';
$scope.data.textvalt[2] = '';
$scope.data.textvalt[3] = '';
$scope.submit = function() {
var configObject = {
method: 'POST',
url: 'index.php',
data: { textval1: $scope.data.textvalt[1] , textval2 : $scope.data.textvalt[2], textval3 : $scope.data.textvalt[3] }
}
$http(configObject).then
(function successCallback(response) {
console.log("posted sucuessfully");
},
function errorCallback(response) {
console.log("Failed");
});
};
}]);

Passing hidden value in angular

View:
<ul ng-repeat="x in posts.post">
{{x.name}} {{x._id}} {{x.post}} {{x.user_id}}
<br>
<ul ng-repeat="y in x.comment">
{{y.comment}}
</ul>
<input type="text" style="display: none;" ng-model='new_comment.userId' value={{users2.id}} name="userId" >
<input type="text" style="display: none;" ng-model='new_comment.name' value={{users2.name}} name="userName" >
<textarea ng-model='new_comment.comment' name="comment" rows="4" cols="50">
</textarea>
<br>
<input type="submit" value="Post Comment!" ng-click="addComment(x._id, new_comment)">
</ul>
Controller:
UserFactory.getUser(function (data) {
$scope.users2 = data;
});
Factory:
factory.getUser = function(callback) {
$http.get("/user").success(function(output) {
users2 = output;
callback(users2);
});
};
I am trying to pass the hidden values of users2.id and users2.name from the controller/factory into a form. I tried ng-init, ng-value as well as input type="hidden", but none works.
So this is what I did to get it working:
View:
<form>
<textarea ng-model='new_comment.comment' name="comment" rows="4" cols="50">
</textarea>
<br>
<input type="submit" value="Post Comment!" ng-click="addComment(x._id, new_comment, users2.name, users2._id)">
</form>
Controller:
$scope.addComment = function(id, comment, userName, userId) {
var commentValue = comment.comment;
var newComment = {comment: commentValue, name: userName, userId: userId};
postFactory.addComment(id, newComment, function () {
postFactory.getComment(function (data) {
$scope.comments = data;
});
$scope.new_comment = {};
});
};
Try this
<input type="hidden" ng-model='new_comment.userId' value="{{users2.id}}" name="userId" >
And while you are changing things
UserFactory.getUser
.then(function (data) {
$scope.users2 = data;
});
with
factory.getUser = function() {
return $http.get("/user");
};
as $http returns a promise
Two way binding wouldn't work with hidden element, so I'd use ng-value to set the value of type="hidden" element
<input type="hidden" ng-value='new_comment.userId' name="userId"/>
<input type="hidden" ng-value='new_comment.name' name="userName"/>
Do something like this, return the promise.
factory.getUser = function(callback) {
return $http.get("/user")
};
Controller:
UserFactory.getUser().success(function(output) {
$scope.users2 = output.data;
});

Resources