AngularJS: after ng-change 1st select is getting empty - angularjs

I am using following code to get data from local json file and populating two dropdowns from it but after ng-change event fires 1st dropdown is empty.
$http({method: 'GET', url: 'data/local.json'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
var _data=data;
$scope.places=[];
$scope.toPlaces=[];
$scope.places=data;
$scope.getToPlaces=function(){
var selected=$('#fromSelect :selected').val();
console.log($scope.places[1]);
var key=$scope.places[1]
$scope.toPlaces = _data[key].to;
}
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert("failure");
});
I am using code from this link: https://github.com/marcoslin/angularAMD and passing json file from my local machine.
After ng-change 1st select is getting empty.

The problem is within your markup:
From angular docs:
When an item in the menu is selected, the array element or object property represented by the selected option will be bound to the model identified by the ngModel directive.
This is the reason why it overrides the places array when it is selected.
The ngModel directive inside a select tag should just refer to another variable on the scope:
<select ng-model="selectedPlace" ng-change="getToPlaces()" id="fromSelect">
<option value="">--Please select From place</option>
<option ng-repeat="p in places" value="{{p.id}}">
{{p.name}}
</option>
</select>
Now instead of var selected=$('#fromSelect :selected').val(); , You can just refer to that varaible:
var selected = $scope.selectedPlace;

Related

Show JSON data in multi select box

I got some problem as I am new in Angular. I tried to show one of the JSON key value in multi select box options but failed to load.
I had gone through the example over here and I am doing same function by clicking button.
http://plnkr.co/edit/TdueXL8BQTi8DOodnD5q?p=preview
So the controller part :-
$scope.master = {};
$scope.addValue = function(ff) {
$scope.master = angular.copy(ff);
$http.post("url", $scope.master).success(function(data) {
$scope.use= data;
console.log("Successful");
},function (data, status, headers, config) {
console.log("something went wrong");
});
console.log($scope.master);
};
So my master={} is binding user input in JSON value ie; {"Name":"","Motor":"","Company":""} . I want to show Name only, on repeat.
View is :-
<select multiple="multiple" id="reg_select_multiple" class="form-control">
<option ng-repeat="x in master" value="{{x.name}}"></option>
</select>

AngularJs : how to trigger ng-change event for defaut selected value of ng-otons in dropdown

I have two dropdowns. Based on the selection of first dropdown value, second dropdown will populate its values. After selecting value from second drop down value, it will call ng-change event and fetching data and displaying the info in table. This is working fine.
But what I need is, in second down , I have to set a default value selected, need to trigger ng-change and display data in table.
Below is HTML code for dropdowns.
<SELECT ng-model="selectedRS" ng-options="x as x.rsName for x in rsData" data-ng-change="getSPList(selectedRS)">
<SELECT ng-model="selectedSP" ng-options="x as x.displayName for x in spInfoList" ng-change="getSPInfo(selectedSP)"></SELECT>
Here I have a table to display info which will come from getSPInfo method
my controller method getSPInfo is
$scope.getSPInfo = function(selectedSubproduct) {
$http({
method: 'GET',
url: getSPInfo.web ',params: {rsId: selectedSP.rsId,spId : selectedSP.id}}).
success(function(data, status, headers, config) {
$scope.spInfo = data;
}).
error(function(data, status, headers, config) {
});
}
Thanks in advance!!
This is just one of those things you'll have to manually trigger. ng-change isn't triggering it because the value isn't being watched until after the controller is being initialized.
In your controller you can either manually invoke the function:
$scope.getSPInfo(your_default_value);
Or what I like to do is wrap it in an onInit function so that it is clear to anyone reading the code what is happening. You can also put in here any other code that needs to run when the controller is initialized.
function onInit(){
$scope.getSPInfo(your_default_value);
}
onInit();
A third option if you're using ui-router for your routing is to put it in the onEnter function as part of the state definition object. This will run the function when you enter that state.

dropdown value disappears when select

I have one select in which I bind data from database by get method.it binds data perfectly in select.but when I select one of it option .its disappears..any help appreciated.
This is my HTML Code:
<select class="form-control" ng-model="MainCategory" ng-options="main.Name for main in MainCategory track by main.ID" placeholder="Select Main Category">
<option value=""></option>
</select>
this is my contoller code
var baseURL = 'http://localhost:50928/api/ProductAPI/';
var MainCategory = [];
url = baseURL + "GetMainCategoryList";
$http.get(url)
.success(function (data) {
$scope.MainCategory = data;
console.log(data);
}).error(function (data) {
console.log(data);
});
Your ng-options data is MainCategory and your ng-model is binded to it as well. This means that selecting an options turns your data to one value only - your selected option. In your case you should have a data property, lets say - categories.
Like this:
ng-options="main.Name for main in categories track by main.ID"
In addition you will hold in your controller another property for the selected Category and ng-model will bind to it:
ng-model="selectedCategoty"

Ng-repeat with search filter only works on 2nd character keyup

I'm using a $http get request in my angular application to access data to bind to my view in a loop using ng-repeat and a filter based on a users input value for a simple search. I'm able to access the data and filter and bind the results to my view but the search only happens when the second character the user types is on keyup, not the first character. I want my search to happen on the first keyup.
MY CONTROLLER:
angular.module("app.findfriendsApp").controller("FindfriendsCtrl", [
'$scope', '$http',
($scope, $http)->
# Get Request.
$http.get('find_friends.json').success((data, status, headers, config)->
$scope.friends = data;
).error((data, status, headers, config)->
log error
)
])
MY VIEW:
main ng-app="app.findfriendsApp"
.container ng-controller="FindfriendsCtrl"
.row
.col-xs-12
input[type="text"
ng-model="search"
placeholder="Search by Name"]
.col-xs-12
ul
li[ng-repeat="friend in filteredData = (friends | filter:search)"
ng-hide="filteredData.length==friends.length"]
|{{friend.username}}

ng-change does not trigger function

I've got an app that has two separate selects with bound properties. I want the user to be able to change the selected option on either of the selects, which will trigger a function. But the function will use the value of the selected option from BOTH of the selects to send to an $http call for database query. Here are code snippets:
html:
<div class="yearFilter">
<label>Working Year : </label>
<select ng-model="filterYear" ng-options="year.code as year.code for year in filterYears | unique:'code'" ngclick="setWorkingData({{filterSport}},{{filterYear}})">
</select>
</div>
<div class="sportFilter">
<label>Working Sport : </label>
<select ng-model="filterSport" ng-options="sport.code as sport.desc for sport in filterSports | unique:'code'" ngclick="setWorkingData({{filterSport}},{{filterYear}})"></select>
</div>
js:
var PermitsCtrl = function ($scope, $http) {
var init = function() {
$http({
...
}).success(function(data) {
...
setWorkingData($scope.filterYear,$scope.filterSport);
}).error(function(error){
...
});
}
var setWorkingData = function (year, sport) {
$http({
url ....
...
data : "action=2&year_code="+year+"&sport_code="+sport,
...
}).success(function(data) {
$scope.workingData = data.workingData;
}).error(function(error) {
...
});
}
init();
}
Upon loading the page, init() is being triggered successfully, which builds the select options and in turn triggers the setWorkingData() function correctly, which changes other values in the DOM accordingly, based on the values in the two selects (filterYear and filterSport) sent by the calling init() function. All is good so far. But changing the value in either select does not trigger the setWorkingData function. The html that is rendered looks correct:
<select ng-model="filterYear" ng-options="year.code as year.code for year in filterYears | unique:'code'" ng-change="setWorkingData(15,FB)" class="ng-valid ng-dirty"><option value="0" selected="selected">14</option><option value="1" selected="selected">15</option></select>
When I change the selected option for either select in the browser, the parameter values for the setWorkingData() function are rendered correctly in the DOM for both selects, but the function is never triggered.
setWorkingData() is defined with private access in the controller. In order for it to be accessible from the view, it must be declared on the scope:
$scope.setWorkingData = setWorkingData;
BTW, you don't need {{ }} for the arguments in ng-change/ng-click:
ng-change="setWorkingData(filterSport, filterYear)"

Resources