AngularJS required attribute doesn't work on select - angularjs

I have a following problem with select in my AngularJS application.
I have set required attribute but it doesn't work on select tag. Doesn't
trigger native validation. Here is my html code:
<div ng-app="app">
<div ng-controller="testCtrl">
<form ng-submit="selectChanged()">
<select ng-model="first" required="required">
<option>choose</option>
<option value="2">2</option>
</select>
<input type="submit" value="test"/>
</form>
</div>
</div>
Here is my controller:
angular.module('app', []).controller('testCtrl', function ($scope) {
$scope.selectChanged = function () {
};
});
Here is working jsfiddle:
http://jsfiddle.net/zono/k3b5J/3/
Best regards.

Your code should be:
angular.module('app', []).controller('testCtrl', function ($scope) {
$scope.first = "";
$scope.selectChanged = function () {
alert($scope.first);
};
});
You didn't add the variable your select is bound to, $scope.first in this case.
And the empty option should have a value of "" --> <option value ="">choose</option>
Here is a working fiddle: http://jsfiddle.net/k3b5J/4/

You need to wire-up form validity with submit button, e.g. so it gets disabled when form is not valid
<div ng-app="app">
<div ng-controller="testCtrl">
<form name="myform" ng-submit="selectChanged()">
<select ng-model="first" required="required">
<option>choose</option>
<option value="2">2</option>
</select>
<input type="submit" value="test" ng-disabled="!myform.$valid"/>
</form>
</div>
</div>
I updated your fiddle to reflect that: http://jsfiddle.net/qt8z3/

The thing is that when option value is not defined, the text is taken as a value, so, when choose is selected, actual value of first should be choose.
Your code can be fixed like this:
<div ng-app="app">
<div ng-controller="testCtrl">
<form ng-submit="selectChanged()">
<select ng-model="first" required="required">
<option value="">choose</option>
<option value="2">2</option>
</select>
<input type="submit" value="test"/>
</form>
</div>
</div>
note value on <option value="">choose</option>
Edit note: when choose is selected, value actually exists, so field is filled and it passes required validation.

Related

AngularJS - Select tag's ng-model value not showing up

I am using AngularJS. I have a select tag. The tag's value is bound by ng-model. The ng-model variable has a value (coming from server). That value does not show up in the select dropdown tag. It shows up in a normal text box though.
How can I get the ng-model value ('blue' for below example) show up in the dropdown?
Please refer - http://jsfiddle.net/9w5XT/2446/
<select ng-model="blisterPackTemplateSelected"
data-ng-options="blisterPackTemplate as blisterPackTemplate.name for
blisterPackTemplate in blisterPackTemplates">
<option value="">Select Account</option>
</select>
<hr>
<input type="text" ng-model="blisterPackTemplateSelected" />
It should be
<select ng-model="blisterPackTemplateSelected"
data-ng-options="blisterPackTemplate.name as blisterPackTemplate.name for
blisterPackTemplate in blisterPackTemplates">
Check this fiddle
I think you must have the same model in array which you use in dropdown like this which you use for selected.
<div ng-app="App" >
<div ng-controller="ctrl">
<select ng-model="blisterPackTemplateSelected"
data-ng-options="blisterPackTemplate.name as blisterPackTemplate.name
for blisterPackTemplate in blisterPackTemplates">
<option value="">Select Account</option>
</select>
<hr>
<input type="text" ng-model="blisterPackTemplateSelected" />
</div>
</div>
var app=angular.module('App', []);
function ctrl($scope){
$scope.itemList=[];
$scope.blisterPackTemplateSelected = 'blue'
$scope.blisterPackTemplates=[{id:1,name:"a"},{id:2,name:"b"},{id:4,name:"c"},{id:3,name:"blue"}]
}

Doing filter by select value is not working

Not able to add the selected value to the filter in angularjs im getting this error(Error: Unexpected token .)
Inside controller
$scope.search_type_options=[{name:"Name",value:"name"},{name:"Id",value:"id"},{name:"Update By",value:"updated_by"}];
$scope.search_val = function()
{
$scope.fs={$scope.search_type.value:$scope.search_txt};
$scope.filter_dt=$filter('filter')($scope.temp_data,$scope.fs);
}
I need output like
{name:"raj"}
Inside Template
<div class="form-group">
<select name="search_type" ng-model="search_type" ng-options="item as item.name for item in search_type_options">
<option value="">--select search type</option>
</select>
</div>
<div class="form-group">
<input type="text" ng-model="search_txt" class="form-control" placeholder="Search">
</div>
<button type="submit" ng-model="search_btn" ng-click="search_val()"class="btn btn-default">Go</button>
</div>
not sure if this is a correct js syntax
$scope.fs={$scope.search_type.value:$scope.search_txt};
normally it should be written like this
$scope.fs = {};
$scope.fs[$scope.search_type.value] = $scope.search_txt;

How can i do an ng-repeat by a given select option value

So my problem is as follows, i have this HTML
<div class="col-sm-4">
<p><span>Teste A/B:</span></p>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="true" ng-model="isTestAvailable"> Sim </label>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="false" ng-model="isTestAvailable"> Não </label>
</div>
<div class="col-sm-4">
<p><span>Nº de testes:</span></p>
<select class="form-control" ng-model="tests.number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
What i'm trying to do is make it so that when the user changes the select to another value i generate the given amount of test inputs via ng-repeat
<div ng-if="isTestAvailable == 'true'" ng-repeat="test in tests">
<hr>
<div class="row">
<div class="col-sm-12">
<h3>Test {{$index}}</h3>
<p><span>Specifications:</span></p>
<textarea id="teste1" class="form-control" onkeyup="resizeTextarea('test1')" data-resizable="true"></textarea>
</div>
</div>
<hr>
</div>
My controller contains this $watch
$scope.$watch("tests.number", function(newVal, oldVal) {
$scope.tests = [{}];
var i = parseInt(newVal);
for(var x = i; x <= newVal; x++) {
$scope.tests.push({});
}
});
that i tried to use in order to change the amount of test objects when the value changes, i tried using a simple variable as well and doing ng-repeat("i in tests.number") instead but that didn't work, what can i do so that the ng-repeat works with my given select option value?
I suggest to you, to use another variable instead of tests.number because you override it in your watcher.
Please check this plunker : https://plnkr.co/edit/XnV9MKjIYe1YOL4hR6Le?p=preview
I use another variable and bind ng-change instead of watch
<select class="form-control" ng-model="testNumber" ng-change="changeTestNumber()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

AngularJS validation is not working

I'm new to AngularJS; can someone help me why the below code is not validating the form?
<form name="reviewForm" ng-controller="ReviewController as reviewCtrl" ng-submit="reviewCtrl.addReview(product);" novalidate>
<select ng-model="reviewCtrl.review.stars" required>
<option value="">Rate the Product</option>
<option value="1">1 Star</option>
<option value="2">2 Star</option>
</select>
<textarea ng-model="reviewCtrl.review.body"></textarea>
<input type="email" ng-model="reviewCtrl.review.author" required/>
<div>ReviewForm is: {{reviewForm.$valid}}</div>
<input type="submit" value="Submit" />
</form>
Remove novalidate from your form.
You have novalidate at the beginning of your form which is disabling your browser's native form validation.
Note that novalidate is used to disable browser's native form
validation.
See HTML 'form' novalidate Attribute and Angular Forms
At first step: Remove novalidate from your form
and add the submit button like this:
<input type="submit" value="Submit" ng-disabled="isFormValid(reviewForm) />
and in your .js:
$rootScope.isFormValid = function (form) {
return form.$invalid;
};
#Milton I think there is no problem with your code, i have created a plunkr that also shows that there is not any issue with your code.
Here is the code i have written.
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<script>
function myctrl($scope) {
$scope.review = {};
}
</script>
</head>
<body>
<div>
<form name="reviewForm" ng-controller="myctrl as reviewCtrl" ng-submit="reviewCtrl.addReview(product);" novalidate>
<select ng-model="reviewCtrl.review.stars" required>
<option value="">Rate the Product</option>
<option value="1">1 Star</option>
<option value="2">2 Star</option>
</select>
<textarea ng-model="reviewCtrl.review.body"></textarea>
<input type="email" ng-model="reviewCtrl.review.author" required/>
<div>ReviewForm is: {{reviewForm.$valid}}</div>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
Here is the plunkr link of the code:
http://plnkr.co/edit/nVCmukG5abpi1Y4ZHkrq?p=preview
If the issue remains, can you give more description of data you are entering?

How can I check number of elements on a select list and disable radio button?

I would like to:
disable radio button (with name 'advertisementType') when select list (with name 'updateAdvertisement') has got only 1 element
disable submit button (with name 'saveButton') when selected option on select list has got null value
How can I do that in angular?
This is my html:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'">
<option value>Select item</option>
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="4">Audi</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="saveButton" />
</form>
</div>
This is my javascript (but I have no idea):
<script>
function AdvertisementCtrl($scope) {
}
</script>
HTML
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" ng-disabled='carTypes.length<=1' value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-options="item.type for item in carTypes">
<option value>Select item</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="!updateAdvertisement" />
</form>
</div>
JavaScript
function AdvertisementCtrl($scope) {
$scope.carTypes = [
{id:'1', type:'Volvo'},
{id:'2', type:'Saab'},
{id:'3', type:'Mercedes'},
{id:'4', type:'Audi'}
]
}
The JSFiddle is here http://jsfiddle.net/7Jw9B/
Populate the select from an array that you declare in the controller
Check array length and disable the radio button when array length is 1
Disable saveButton based on the value of updateAdvertisement
<script>
function AdvertisementCtrl($scope) {
$scope.cars = ['Volvo', 'Saab', 'Mercedes', 'Audi', 'BMW'];
}
</script>
Markup:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false" ng-disabled="cars.length <= 1">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'" ng-options="cars">
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="updateAdvertisement" />
</form>
</div>
Should probably be tested in a fiddle :-), could do that later

Resources