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>
Related
I have a select tag in html which has a value from angularjs. Everytime I select a value, the select tag will be empty. Why? How to fix it?
$scope.getAll = function(){
$http.post()....
$scope.places = response.data;
};
$scope.getAll();
$scope.sample = function(str){
alert(str);
};
HTML
<select ng-model="val" ng-change="sample(val)">
<option ng-repeat="place in places" value="place.name">{{place.name}}</option>
</select>
In code above, when I select some value of the select tag it alerts me the value but the select tag goes empty!
i recommend instead of using ng-repeat use ng-options
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.places = [ {name: 'California'},
{name: 'New York'},
{name: 'Cochin'}
];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="val" ng-change="sample(val)" ng-options="place.name as place.name for place in places">
</select>
{{val}}
</div>
When setting the value of the option as an expression, try using ng-value .
While your question is confusing, I'm guessing that the alert shows up as place.name
<select ng-model="val" ng-change="sample(val)">
<option ng-repeat="place in places" ng-value="place.name">{{place.name}}</option>
</select>
Or if you want to set the value of the select attribute itself, you can use,
<select ng-model="val" ng-change="sample(val)">
<option ng-repeat="place in places" value="{{place.name}}">{{place.name}}</option>
</select>
https://plnkr.co/edit/fAbqV1wG2VuF069lt3bm?p=preview
This might be able to guide you further.
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
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.
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 )
}
My model is the array vh.loadedViews with 4 items.
My ng-repeat on the option: ng-repeat="view in vh.loadedViews"
my ng-model on the select: ng-model="view.description"
However in my ng-change function vh.chooseView it's printing out the binding text and not the actual model data.
Markup:
<section class="saved-views-modal" ng-show="vh.loadSavedModal">
<header><h1>Select a saved view</h1></header>
<select name="select"
ng-model="view.description"
ng-change="vh.chooseView(view.description)">
<option ng-repeat="view in vh.loadedViews"
value="view.description">{{view.description}}</option>
</select>
<button class="btn-green"
ng-click="vh.loadSavedView(view.description)">
Load View
</button>
</section>
Directive Controller:
var vm = this; // vh in markup
vm.chooseView = function(description) {
console.log('chooseView > view.description: ');
console.log(description);
}
You have not populated value tag of your options. You should populate it using interpolation like value="{{view.description}}" to render option value.
Also you ng-model should be ng-model="vh.description" instead of view.description
Markup
<select name="select"
ng-model="vh.description"
ng-change="vh.chooseView(view.description)">
<option ng-repeat="view in vh.loadedViews"
value="{{view.description}}">{{view.description}}</option>
</select>