Can not transfer to an array in angularJs - angularjs

When registering a user, I want to immediately define the role for it, for this I added a scroll, but since the roles field in Java is an array, I pass this value as an array, and so, I cannot understand why this value is not assigned
<div class="bloc">
<select name="role" size="">
<option value=["ROLE_USER"] ng-model="user.roles">ROLE_USER</option>
<option value=["ROLE_SUPPORT"] ng-model="user.roles">ROLE_SUPPORT</option>
</select>
</div>
As you understand, I cannot add a user like that, but if I add $scope.user.roles = ["ROLE_USER"] in the controller;
then the value is assigned to the roles field, and no problems arise, what would you advise on this issue? I get 400 error.
Can send more info if needed
<option value=["ROLE_USER"] ng-options="user.roles">ROLE_USER</option>
is it right version?
<label>Color grouped by shade, with some disabled:
<select ng-model="myColor"
ng-options="color.name group by color.shade disable when color.notAnOption for color in colors">
</select>
</label><br />
that example from link

Related

Show a select field with options YES/NO without having a default option selected

I'm working on angularjs and I have a field that has two options YES/NO.
When someone will fill this field I need to show the two options but non of them is selected. So it will look like an empty value, but this empty value should not be an option. I tried to add <option value="?"></option> in the following code, but then the empty value is an option.
Any ideas?
<div class="col-md-3" ng-if="obj.options.length > 0">
<select class="form-control" ng-model="patientReportedSymptomsUI[key]" ng-disabled="$root.newVisit">
<option value="?"></option>
<option ng-repeat="option in obj.options">{{option}}</option>
</select>
</div>

Why the Angular ng-options value contains the value data type?

Guys after I used the angular directive ng-options I got this result
<select name="users" id="users" ng-options="user.id as user.name for user in users" ng-model="user">
<option value="number:1" label="Developers">Ahmed</option>
<option value="number:2" label="Designers">Jon</option>
<option value="number:3" label="HR">Astm</option>
<option value="number:4" label="Doctor">Fady</option>
</select>
<select name="colors" id="colors" ng-options="color.code as color.name for color in colors" ng-model="color">
<option value="string:ff0000" label="Developers">Red</option>
<option value="string:ffffff" label="Designers">White</option>
<option value="string:000000" label="HR">Black</option>
</select>
why the option value contains the data type such as number:1 or string:ffffff ??
how I can remove the data type from it and keeping only the value ?
Add track by [id] at the end of ng-options.
In your case it would be:
ng-options="user.id as user.name for user in users track by user.id"
and
ng-options="color.code as color.name for color in colors track by color.code"
Right now there is not option in angular to remove that type. If you need that, use custom directive with ng-option as parent.

AngularJS - Default Dropdown Select Option

This one issue is bugging me since it seems so easy to fix.
I am using plain HTML, but the data is being passed to the next view via angular.
The snippet of code looks like this:
<span data-ng-show="isCountry()">
<select class="selectpicker" data-ng-model="age">
<option value="21">21-24</option>
<option value="25">25-30</option>
<option value="31">31 and up</option>
</select>
</span>
What I am trying to achieve:
Having <option value="25">25-30</option> as the default selected option when angular loads
What I've tried:
Having <option value="25">25-30</option> with a ng-selected="age" attribute
<span data-ng-show="isCountry()">
<select class="selectpicker" data-ng-model="age">
<option value="21">21-24</option>
<option ng-selected="age" value="25">25-30</option>
<option value="31">31 and up</option>
</select>
</span>
Having <option value="25">25-30</option> with a selected="selected" attribute
<span data-ng-show="isCountry()">
<select class="selectpicker" data-ng-model="age">
<option value="21">21-24</option>
<option selected value="25">25-30</option>
<option value="31">31 and up</option>
</select>
</span>
Even trying <option value="25">25-30</option> displayed first
<span data-ng-show="isCountry()">
<select class="selectpicker" data-ng-model="age">
<option value="25">25-30</option>
<option value="21">21-24</option>
<option value="31">31 and up</option>
</select>
</span>
Option 2 and 3 does display the value I want first, but it is not passed through to the next view
If possible, I prefer not to touch much Angular code, as the main developer is out temporary...
Any help would be appreciated.
Thanks!
Apparently, After much digging into the code the - the age is already being set via a js function
a.defaultAge = {
Country: "25"
}
Thanks for the help David - Your comment is a good to know thing for future reference.
There are a problem here, do not use 'age' as the model but try using something like 'user.age' and assign the value to it like this;
var user = {}; user.age = 25;
<select class="selectpicker" data-ng-model="user.age">
The reason it does not work in the next view is, that the 'age' is totally gone when you reassign a new value to it. Angular monitor change to a variable reference, since the variable 'age' is gone, it does not know when to update the view. Using 'user.age' as the model, angular is watching the 'user' for changes, and if you change the age, Angular will update the view for you.

returning multible objects with a property of >= number

I have managed to filter out what i want, the problem is it only brings back the first object in the database that matches it. i am trying to have a select box with filter by the job salary minimum. $50,000 $60,000 and so on. for example, when i select the $50,000 option the only result returned is jobs that have there salary set at exactly 50000. I need everything returned that is above 50000. any help is appreciated.
Controller
$scope.salaryFilter = function (min) {
return min.salaryMin >= $scope.salary_Min ;
}
View
<label>Salary Desired:</label><br />
<select class="form-control" ng-model="search.salaryMin">
<option value="">Select</option>
<option ng-repeat="salary in jobArray | filter:salaryFilter"></option>
<option ng-model="salary_salaryMin" value="50000">$50,000</option>
<option ng-model="salary.salaryMin" value="60000">$60,000</option>
<option ng-model="salary.salaryMin" value="70000">$70,000</option>
<option ng-model="salary.salaryMin" value="80000">$80,000</option>
<option ng-model="salary.salaryMin" value="90000">$90,000</option>
<option ng-model="salary.salaryMin" value="100000">$100,000</option>
</select><br />
Here's a plunker with a basic example of how to do this.
From the behaviour you describe, it sounds like you may be matching strings rather than integers, but we won't know if you don't show us more code
Controller:
$scope.data = [
{
"val":27000,
"text":"$27,000"
},{
"val":29000,
"text":"$29,000"
},{
"val":50000,
"text":"$50,000"
},{
"val":70000,
"text":"$70,000"
}];
$scope.name = "John";
$scope.salaryMin = 27000;
$scope.salaryFilter = function(salary) {
if (salary.val >= $scope.salaryMin)
return true;
}
Template:
<select >
<option ng-repeat="salary in data | filter: salaryFilter" value="{{ salary.val }}">{{ salary.text }}</option>
</select>
Changing the value of $scope.salaryMin will filter the data
Edit
The problem is shown in this plunker, and i've created a fork here which fixes the issue
There were a couple problems
We're setting the individual salary options manually in the option list, so there's no need for the ng-repeat in our salary select box. We also need this select box to set the $scope.salary_Min variable, so we shouldn't have ng-model set to salary.salaryMin on the option tags.
<!-- REMOVE THIS -->
<option ng-repeat="salary in jobArray | filter:salaryFilter"></option>
<!-- REMOVE ng-model FROM these -->
<option ng-model="salary_salaryMin" value="50000">$50,000</option>
<option ng-model="salary.salaryMin" value="60000">$60,000</option>
<option ng-model="salary.salaryMin" value="70000">$70,000</option>
<option ng-model="salary.salaryMin" value="80000">$80,000</option>
<option ng-model="salary.salaryMin" value="90000">$90,000</option>
<option ng-model="salary.salaryMin" value="100000">$100,000</option>
Because we want the select box to set the $scope.salary_Min variable (which is used in our filter), we add ng-model to the select tag
<select class="form-control" ng-model="salary_Min">
We previously had the salaryFilter on our option tag, but we want to use it to filter the table. So we add it to the table where we're using ng-repeat. It's okay to keep the existing search filter because filters in Angularjs are stackable
<tr ng-repeat="job in jobArray | filter:search | filter:salaryFilter">
<td>{{job.jobTitle}}</td>
<td>{{job.companyName}}</td>
<td>{{job.description}}</td>
<td>{{job.city}}</td>
<td>{{job.salaryMin}}</td>
</tr>
This is nearly done. The one issue left is that these values are being interpreted as strings by our filter, which can cause some confusing results. We just need to make sure they're interpreted as integers using parseInt()
$scope.salaryFilter = function (min) {
return parseInt(min.salaryMin) >= parseInt($scope.salary_Min) ;
};

How to set the value attribute for select options?

Source JSON data is:
[
{"name":"Alabama","code":"AL"},
{"name":"Alaska","code":"AK"},
{"name":"American Samoa","code":"AS"},
...
]
I try
ng-options="i.code as i.name for i in regions"
but am getting:
<option value="?" selected="selected"></option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
while I am expecting to get:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AS">American Samoa</option>
So, how to get value attributes and get rid of "?" item?
By the way, if I set the $scope.regions to a static JSON instead of AJAX request's result, the empty item disappears.
What you first tried should work, but the HTML is not what we would expect. I added an option to handle the initial "no item selected" case:
<select ng-options="region.code as region.name for region in regions" ng-model="region">
<option style="display:none" value="">select a region</option>
</select>
<br>selected: {{region}}
The above generates this HTML:
<select ng-options="..." ng-model="region" class="...">
<option style="display:none" value class>select a region</option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
</select>
Fiddle
Even though Angular uses numeric integers for the value, the model (i.e., $scope.region) will be set to AL, AK, or AS, as desired. (The numeric value is used by Angular to lookup the correct array entry when an option is selected from the list.)
This may be confusing when first learning how Angular implements its "select" directive.
You can't really do this unless you build them yourself in an ng-repeat.
<select ng-model="foo">
<option ng-repeat="item in items" value="{{item.code}}">{{item.name}}</option>
</select>
BUT... it's probably not worth it. It's better to leave it function as designed and let Angular handle the inner workings. Angular uses the index this way so you can actually use an entire object as a value. So you can use a drop down binding to select a whole value rather than just a string, which is pretty awesome:
<select ng-model="foo" ng-options="item as item.name for item in items"></select>
{{foo | json}}
If you use the track by option, the value attribute is correctly written, e.g.:
<div ng-init="a = [{label: 'one', value: 15}, {label: 'two', value: 20}]">
<select ng-model="foo" ng-options="x for x in a track by x.value"/>
</div>
produces:
<select>
<option value="" selected="selected"></option>
<option value="15">one</option>
<option value="20">two</option>
</select>
If the model specified for the drop down does not exist then angular will generate an empty options element. So you will have to explicitly specify the model on the select like this:
<select ng-model="regions[index]" ng-options="....">
Refer to the following as it has been answered before:
Why does AngularJS include an empty option in select? and this fiddle
Update: Try this instead:
<select ng-model="regions[index].code" ng-options="i.code as i.name for i in regions">
</select>
or
<select ng-model="regions[2]" ng-options="r.name for r in regions">
</select>
Note that there is no empty options element in the select.
You could modify you model to look like this:
$scope.options = {
"AL" : "Alabama",
"AK" : "Alaska",
"AS" : "American Samoa"
};
Then use
<select ng-options="k as v for (k,v) in options"></select>
It appears it's not possible to actually use the "value" of a select in any meaningful way as a normal HTML form element and also hook it up to Angular in the approved way with ng-options. As a compromise, I ended up having to put a hidden input alongside my select and have it track the same model as my select, like this (all very much simplified from real production code for brevity):
HTML:
<select ng-model="profile" ng-options="o.id as o.name for o in profiles" name="something_i_dont_care_about">
</select>
<input name="profile_id" type="text" style="margin-left:-10000px;" ng-model="profile"/>
Javascript:
App.controller('ConnectCtrl',function ConnectCtrl($scope) {
$scope.profiles = [{id:'xyz', name:'a profile'},{id:'abc', name:'another profile'}];
$scope.profile = -1;
}
Then, in my server-side code I just looked for params[:profile_id] (this happened to be a Rails app, but the same principle applies anywhere). Because the hidden input tracks the same model as the select, they stay in sync automagically (no additional javascript necessary). This is the cool part of Angular. It almost makes up for what it does to the value attribute as a side effect.
Interestingly, I found this technique only worked with input tags that were not hidden (which is why I had to use the margin-left:-10000px; trick to move the input off the page). These two variations did not work:
<input name="profile_id" type="text" style="display:none;" ng-model="profile"/>
and
<input name="profile_id" type="hidden" ng-model="profile"/>
I feel like that must mean I'm missing something. It seems too weird for it to be a problem with Angular.
you can use
state.name for state in states track by state.code
Where states in the JSON array, state is the variable name for each object in the array.
Hope this helps
Try it as below:
var scope = $(this).scope();
alert(JSON.stringify(scope.model.options[$('#selOptions').val()].value));

Resources