AngularJS - ng-change not firing - javascript

I'm setting the ng-change directive in a select element of my form but the function linked to it is not called when I change the value. I've been reading very similar questions and applying the answers I see and so far none has worked yet.
Can you see wjat I'm doing wrong?
My HTML:
<div class="row" ng-app="quoteApp">
<div class="col-lg-12" ng-controller="QuoteController" ng-init="initialize()">
<h1 class="page-header">Quote</h1>
<div class="form-group">
<label>Carrier</label>
<select class="form-control" ng-model="form_carrier_id" ng-change="loadProducts()">
<option ng-repeat="carrier in carriers" value="{{carrier.id}}">{[{carrier.name}]}</option>
</select>
</div>
<div class="form-group">
<label>Product</label>
<select class="form-control" ng-model="form_product_id">
<option value=""></option>
<option ng-repeat="product in products" value="{{product.id}}">{[{product.name}]}</option>
</select>
</div>
</div>
</div>
And my controller:
angular.module('quoteApp', ['angular-loading-bar']).config(function($interpolateProvider){
$interpolateProvider.startSymbol('{[{').endSymbol('}]}');
})
.controller('QuoteController', function ($scope, $http) {
$scope.carriers = [];
$scope.products = [];
$scope.form_carrier_id = '';
$scope.form_product_id = '';
$scope.getUrl = function(action){return '/admin/application/quote/json?action='+ action;}
$scope.initialize = function(){
// Get the list of carriers
$http.get($scope.getUrl('getCarriers'))
.success(function (data) {
$scope.carriers = data;
})
.error(function () {
alert('Error loading carriers');
});
}
$scope.loadProducts = function(){
alert('HERE');
}
});
For me everything looks right. Can you see what I'm missing? The first select loads normally, the problem is that when I change its value the function loadProducts is not fired.
Thanks

this may not be the problem itself, however you should always use ng-options rather than <option ng-repeat>
I've seen some weird behaviour in the past due to that

I have found what was the problem. It was how I was filling the select. It has to be filled like this:
<select class="form-control" ng-model="form_carrier_id"
ng-options='carrier.id as carrier.name for carrier in carriers'
ng-change="loadProducts()">
</select>

Try using ng-options for repeating the products in your select element.
You can use ng-options as follows:
<select class="form-control" ng-model="form_product_id"
ng-options="product.id as product.name for product in products" ng-change="loadProducts()">
<option value="">Select color</option>
</select>
not as the products are set values, the loadProduct should trigger, whenever a value is selected as the ng-model would be set

Related

How to get value of sibling select on btn click?

I want to get the value of selected option on button click.
Problem is that I have several groups of button and select. So, I have to use sibling somehow, but I cant figure out how to.....
<div class="form-group" ng-repeat="category in categories">
<div id="kkal-type" class="form-options">
<select name="{{cats[$index]}}" id="{{cats[$index]}}">
<option value="0" >--Не выбрано--</option>
<option ng-repeat="food in category.foods" ng-show="food.available" value="{{food.cost}}">{{food.name+" - "+food.cost+"тг"}}</option>
</select>
<input type="button" value="ВЫБРАТЬ" class="choose-btn orange filled" ng-click="update(this)"/ >
</div>
$scope.update = function(btn) {
console.log( $(btn).siblings('select').val());
};
But this doesnt work.
Who knows how to get this working?
Thank you in advance!
just use ng-model on select and pass that value to ng-click function
<select name="{{cats[$index]}}" id="{{cats[$index]}}" ng-model="selectedItem">
<input type="button" value="ВЫБРАТЬ" class="choose-btn orange filled" ng-click="update(selectedItem)"/ >
$scope.update = function(selectedItem) {
console.log( selectedItem);
};
on the side note, use ng-options instead of ng-repeat
<select name="{{cats[$index]}}" id="{{cats[$index]}}" ng-options="food.cost as food.name for food in category.foods" ng-model="selectedItem">
<option value="0" >--Не выбрано--</option>
</select
Just navigate to the parent: $.parent() and then search downwards: $.find("select").
You can also use the $.closest() method, this will look in the tree and find the closest element that suits your selector, which is what you need:
$scope.update = function(btn) {
console.log( $(btn).closest('select').val());
};

Call a function in a select element for filling ngOptions source?

I have a <select> as below:
<div ng-show="myCtrl.name == 'Jack'">
<select ng-model="myCtrl.selectedValue" ng-options="value.id as value.title for value in myCtrl.valueDrpValues">
<option value=""> Select One ...</option>
</select>
</div>
And in myController.js, I have a function which should fill the myCtrl.valueDrpValues. how can I call this function from the select tag? I tested the ng-init, but it didn't work. Also, I called the function exactly from the ng-option and it didn't work, too. (as below:)
ng-options="value.id as value.title for value in definitionCtrl.fillValueDrp()"
You can use ng-init
Call your function there and your array will be populated, for example like so:
<div ng-init="fillValueDrp()">
<div ng-show="myCtrl.name == 'Jack'">
<select ng-model="myCtrl.selectedValue" ng-options="value.id as value.title for value in myCtrl.valueDrpValues">
<option value=""> Select One ...</option>
</select>
</div>
</div>
try this.
var app = angular
.module('MyApp', [
])
.controller('Main', ['$scope', function ($scope) {
var self = this;
self.items = [];
self.name = "Jack";
self.fillValueDrp = function(){
for(var i=0 ;i < 5; i++){
self.items.push({"id":i,"title":i});
}
}
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="main-content" ng-app="MyApp" ng-controller="Main as myCtrl">
<div ng-show="myCtrl.name == 'Jack'" ng-init= "myCtrl.fillValueDrp()">
<select ng-model="myCtrl.selectedValue "ng-options="value.id as value.title for value in myCtrl.items">
<option value=""> Select One ...</option>
</select>
</div>
</div>

Angularjs - Change value based on siblings

I am trying to have multiple dropdown selects which will decide on specific input values
The HTML
<div ng-repeat="i in [1,2,3]">
<select name="singleSelect" ng-model="chosenitem" style="width:100%;" ng-change="changedValue(i,$index)" >
<option value="" disabled selected>choose item</option>
<option ng-repeat="me in stuffs">{{me.item}}</option>
</select>
<input type="text" ng-model="inputattr[i]" placeholder="attribute">
</div>
The controller
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.stuffs = [
{item:'car', attribute:'wheels'},
{item:'plane', attribute:'wings'},
{item:'boat', attribute:'propeller'}
];
$scope.changedValue = function(i,j){
$scope.inputattr[i] = $scope.stuffs[j].attribute;
};
});
The problem is inputattr is dynamic. I can never get it to work. Here is my plunk.
see this plunkr: http://plnkr.co/edit/GVanAH70oyEEI16Nbf0t
you never actually initialized inputattr. there also was an issue with your indexes, which should usually start at 0.
The reason your code is not working is because inputattr is not initialized. Add
$scope.inputattr = ['','',''];
inside MainCtrl. That will do the trick.

Passing an ng-model value to an object

I have a list made with ng-repeat with options. I'm trying to make it so that when I click an option, the value of the selection will be passed to an object.
<select id="selectvak" name="selectvak" class="form-control" multiple="multiple" >
<option value="1" ng-repeat="t in klas" ng-model="vakselect">{{ t.Name }}</option>
</select>
$scope.user = {
vak: klas.vakselect,
};
I have small experience with Angular, so I'm not sure how to make it work correctly.
Edit:
I have adjusted my code and user.vak does seem to catch the selectvak field, but now passes it as an array. How do I make it so it's a string in $scope.user? So whenever I log user, I will see a string of vak and then an array of students.
<select id="selectvak" name="selectvak" class="form-control" multiple="multiple" ng-model="user.vak" ng-options="t.Name as t.Name for t in klas" ng-click="getJSON1()">
$scope.user = {
vak: '',
// klas: klasselect,
student: [$scope.student] -1
};
TLJ and Arnaud are correct.
But answering your initial question, in the select tag, instead of ng-model="vakselect" just use ng-model="user.vak".
Then, when the value of the select changes, the value of user.vak will change automatically.
real code is this : (and nothing to do inside the controller.
<select id="selectvak" name="selectvak" class="form-control" multiple="multiple" ng-model="user.vak">
<option value="1" ng-repeat="t in klas">{{ t.Name }}</option>
</select>
And you should use ng-options instead of ng-repeat to create your options
https://docs.angularjs.org/api/ng/directive/ngOptions
This will add t.name to an array when you click the option. Take a look at the controller function and the ng-click in the partial.
<select id="selectvak" name="selectvak" class="form-control" multiple="multiple" >
<option value="1" ng-repeat="t in klas" ng-click="add(t.Name)" ng-model="vakselect">{{ t.Name }}</option>
</select>
var string_option;
$scope.add = function(t) {
string_option = t;
console.log(string_option )
}

Get selected option in dynamic Angular dropdown

I want have a simple select:
<ion-view view-title="fc" id="fcs">
<ion-content>
<div id="params-container">
<select ng-model="value1" ng-options="value for value in params1 track by value" ng-change="update()"></select>
</div>
</ion-content>
</ion-view>
And in the controller:
angular.module('filedetails.controller', [])
.controller('FileDetailsCtrl', function($scope, $stateParams, FilesService, ProcessFileService, FCSModel) {
$scope.params1 = ["FSC-A", "FSC-H", "FSC-W", "SSC-A"];
$scope.update = function(){
console.log('scope.axisX is');
console.log($scope.axisX);
}
});
And in app.js I have:
.state('file-detail', {
url: '/files/:id',
templateUrl: 'js/files/details/template.html',
controller: 'FileDetailsCtrl'
})
I want to get the selected value but somehow when i select a value in the dropdown, it always outputs undefined. What can I be doing wrong? Im using AngularJS v1.3.13.
You need to do it like this:
<select ng-model="axisX" ng-options="value as value for value in params1" ng-change="update()"></select>
or like this:
<select ng-model="axisX" ng-options="value for value in params1 track by value" ng-change="update()"></select>
With first approach you will get:
<option value="0">FSC-A</option>
<option value="1">FSC-H</option>
With second approach you will get:
<option value="FSC-A">FSC-A</option>
<option value="FSC-H">FSC-H</option>
Here is the working plunker
I assume your setup is wrong. Your code works on jsfiddle, at least as soon as I add the ng-controller tag: https://jsfiddle.net/wg5dtb8o/
<div ng-controller="MyCtrl">
<select ng-options="value for value in params1" ng-model="axisX" ng-change="update()"></select>
</div>

Categories