Using AngularJS I have a list of selected option and one additional "New" option. What I am trying to do is, when "New" option is chosen it will show a text-box to add a new value.
<select ng-model="group.name" ng-options="bl.Name for bl in Groups" >
<option value="VALUE FROM TEXT BOX">New</option>
</select>
<div class="col-md-4" ng-show="WHEN NEW OPTION IS Chosen ">
<input class="text-box" type="text" name="NewValue" ng-model="group.name" />
</div>
You can try like this
Working Demo
<div ng-app='myApp' ng-controller="ArrayController">
<select ng-model="group.name" ng-options="Group.Name for Group in Groups"></select>
<div class="col-md-4" ng-show="group.name.Value == 'new'">
<input class="text-box" type="text" name="NewValue" ng-model="newValue" />
<button ng-click="add(newValue)">Add</button>
</div>
</div>
Just watch for the selected item change:
$scope.displayDiv = false;
$scope.newval = 'newval';
$scope.$watch('group.name', function(newval, oldval) {
if (newval == $scope.newval) {
$scope.displayDiv = true;
}
}
and for the HTML :
<div class="col-md-4" ng-show="displayDiv">
Here's a working example.
First we set up our options and input.
<select ng-model="curopt"
ng-options="option.name for option in options">
</select>
<input type="text" ng-model="newopt"
ng-show="curopt.name == 'New'"
ng-keypress="newOption($event)">
The ng-show option for our input is tied to the name of the currently selected option. We also add an ng-keypress directive to allow us to handle the enter key being pressed.
Now to the controller...
$scope.newOption = function(event) {
if (event.which == 13) {
$scope.options.push({ name : $scope.newopt, value : $scope.newopt });
$scope.newopt = '';
}
}
Because our select input options is tied to the $scope.options we can simply append our text inputs value to the array and angular will handle updating for us due to data binding.
Related
The below thing is taking too much time than expected. Been through top stack solutions and somehow got the below thing--
WHAT IS THE SCENERIO
I have an ng-repeat div with dropdown.
The drop down contains values and based on selection of those values a div will be shown. What I managed is div is shown. But when I choose another item the previous item div gets hidden.
Below are the screen shot and my code
As it can be seen that when I select the first item it shows the textbox div. But when I select the next item the first gets hidden. There are essentially two values -- ALL, Fixed. When All selected nothing will be shown and when Fixed is selected the div for that particular item will be shown.
HTML code
<div class="tst text-success" ng-repeat="(parentIndex, catList) in categoryArray">
<div class="col-md-3">
{{catList.categoryName}}
</div>
<div class="col-md-4">
<select class="form-control m-b" ng-model="catObj.cats" ng-change="changeOption(catObj,parentIndex)">
<option value="All">All</option>
<option value="fixed">Fixed No. Of Records</option>
<option value="perfixed">% od Fixed</option>
</select>
</div>
<div class="col-md-3 noPad" ng-if="isShowing==parentIndex">
<input type="text" class="form-control form-control-small" placeholder="Set Number" />
</div>
</div>
CONTROLLER
$scope.changeOption = function(obVal,index) {
console.log(obVal);
console.log(index);
if(obVal.cats == "All") {
//$scope.tbx = 0;
}
else {
$scope.isShowing = index;
}
}
Help would be appreciated
Thanks
You're using a single boolean $scope variable, isShowing, to control the visibility of several divs. That can't possibly work.
You should
have an array of objects, each having a selectedOption field.
use ng-model in your select box to set the selectedOption of the object you're editing.
use the value of the object's selectedOption to know if the additional input should be visible for that object.
Example:
<div ng-repeat="obj in objects">
{{ obj.name }}
<select name="op" ng-model="obj.selectedOption" ng-options="option.value as option.value for option in options"></select>
<input ng-if="obj.selectedOption !== 'All'" />
</div>
app.controller('MainCtrl', function($scope) {
$scope.options = [
{value: 'All'},
{value: 'Fixed'}
];
$scope.objects = [
{name: 'Twitter', selectedOption: 'All'},
{name: 'News', selectedOption: 'Fixed'}
]
});
Using AngularJS, there is a requirement to show a dropdown with a selected value:
- When user selects a different value we should show a warning message and shouldn't proceed with update (save button disable).
- When user reverts to the original value, the message should hide and able to update (save button enable)
<select ng-model="vm.sldetails.AgencyGroupID" onchange=""
ng-options="a.AgencyGroupID as a.AgencyGroupName for a in vm.agencygroups">
<option value="">--Select--</option>
</select>
<label ng-show="">Warning message</label>
Instead of calling an event in controller, can we achieve directly in Onchange event?
<div class="col-md-2">
<select class="form-control" ng-model="vm.sldetails.AgencyGroupID"
ng-options="a.AgencyGroupID as a.AgencyGroupName for a in vm.agencygroups">
<option value="">--Select--</option>
</select>
</div>
HTML:
<label class="col-md-2 control-label text-align-left" id="lblWarningMessage" ng-if="vm.sldetails.AgencyGroupID==='your value'">Warning message</label>
In label tag,your value should be replaced by ur compare value
I guess you could try something like this:
<select ng-model="vm.sldetails.AgencyGroupID" ng-change="vm.selectChanged()"
ng-options="a.AgencyGroupID as a.AgencyGroupName for a in vm.agencygroups">
<option value="">--Select--</option>
</select>
<label ng-show="vm.showWarning">Warning message</label>
And in js:
vm.selectChanged = function(){
if(vm.sldetails.AgencyGroupID !== 'the initial value'){
vm.showWarning = true;
}else{
vm.showWarning = false;
}
}
If we have multiple checkbox in ng-repeat and i want to determine in which checkbox is checked or unchecked in controller's action. so i was not able to do it.
<ul class="list-month" id="divMonths">
<li ng-repeat="m in model.Months">
<div>
<input type="checkbox" id="{{m.Month}}" ng-model="$index" class="left" ng-change="onClickMonth($index)" />
</div>
</li>
</ul>
$scope.onClickMonth = function (id) {
if (id=== true) {
alert("true");
}
else {
alert("false");
}
};>
You could also just use the multiple option.
<select multiple="multiple" ng-model="value" ng-options="m in model.Months"> </select>
value will then be an array that is filled as soon as a checkboxed is selected.
I have a div in which I am not using the form tag and submit the form on ng-click button but how can I apply the validation of given filed in AngularJS.
<div ng-controller="AddNewvisaController">
<input type="text" class="form-control" ng-model="visa.requirement">
<select ng-model="visa.country">
<option value="1">abc<option>
<option value="2">xyz<option>
<option value="3">pqrs<option>
</select>
<button type="submit" data-ng-click="submitvisa()">Submit</button>
</div>
You can use the "ng-form" directive if you really dont want to add a form tag.
<body ng-controller="MainCtrl">
<div ng-form="myForm">
<input type="text" required ng-model="user.name" placeholder="Username">
<button ng-click="doSomething()" ng-disabled="myForm.$invalid">DO</button>
</div>
</body>
example
You may try something below,
var myValidationModule = angular.module('myApp', []);
myValidationModule.controller('AddNewvisaController', function($scope) {
$scope.visa = {requirement : "", country : "pqrs"}
//initilize to false - it will make sure the disable the submit button
$scope.enableSubmitButton = false;
//Do the watch collection - whenever something changed, this will trigger and then do the validation as per the needs - here i validated as not empty - you can do whatever you wish and if everything is fine, then enable the button
$scope.$watchCollection(['requirement, country'], function(valueArray) {
if(valueArray[0] != "" && valueArray[1] != "") {
$scope.enableSubmitButton = true;
} else {
$scope.enableSubmitButton = false;
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="AddNewvisaController">
<input type="text" class="form-control" ng-model="visa.requirement">
<select ng-model="visa.country">
<option value="1">abc<option>
<option value="2">xyz<option>
<option value="3">pqrs<option>
</select>
<button type="submit" data-ng-click="submitvisa()" ng-disable="!enableSubmitButton">Submit</button> <!-- introduce the disable directive to make is disable by default and this will acitve if all the required filed had met their validation
</div>
</div>
I'm developing an app in mobile-angular-ui (angular+bootstrap).
In my login page, users can remember their credentials (username and password). Users' data ara saved in the localStorage.
That works fine. I can load users in my login page using datalist:
<form name="userForm">
<div class="form-group" ng-class="{ 'has-error' : submitted && userForm.username.$invalid && !userForm.username.$pristine }">
<label for="inputEmail" class="control-label">Username</label>
<input type="text" class="form-control" id="inputEmail"
name="username" ng-model="user.username" list="UserMemoList"
required>
</div>
<datalist id="UserMemoList">
<option ng-repeat="item in userMemoList track by $index" ng-click="setChange(item)"
value="{{item.username}}" >
</option>
</datalist>
I can select the item.username I want, but I'm not able to select the corresponding item.password.
My problem is that datalist doesn't work like select, and i cannot use ng-options. I have to use option+value to pass my value to the input.
<div class="form-group" ng-class="{ 'has-error' : submitted && userForm.password.$invalid && !userForm.password.$pristine }">
<label for="inputPassword" class="control-label">Password</label>
<input name="password" type="password" class="form-control" id="inputPassword"
ng-model="user.password" required>
</div>
I would be able to use a ng-change="theSelectedUserIs(item)", but I'm not able to pass the item object, because datalist is related to the username input, so I need to pass item.username.
Any idea?
I don't want to use typeahead because actually is deprecated in bootstrap3 or add other libraries/js/css.
I would like to do that only using angular and html.
My controller:
$scope.userMemoList = JSON.parse(localStorage.getItem('userList'));
var user = {};
LoginService.setUser(user);
userMemoList is an array of object like:
{"username":"admin", "password":"admin"}, ...
Thanks.
You can also pass with html data-id like so
in your angular js file
// when input name textbox change, check the value is same with datalist value or not. If value same, then bind that item.password to angular password model.
$scope.$watch "user.username", (newValue) ->
if newValue != null && newValue.lenght > 0
$("#locationlist option").each (index) ->
if $(this).val() == newValue
$scope.user.password = $(this).data("id") // this line will set your password text field
in your view
// pass user password via html5 (data-id) element like below
<datalist id="UserMemoList">
<option ng-repeat="item in userMemoList track by $index" ng-click="setChange(item)"
value="{{item.username}}" "data-id" = {{item.password}} >
</option>
</datalist>
This is how I solved this problem in my app. Use to wrap .
<input class="input form-control" placeholder="Search Conf by Application ID"
list="app_ids" ng-model="confs_app_id"/></th>
<datalist id="app_ids">
<select>
<option ng-repeat="app in app_list" value="{{app}}"></option>
</select>
</datalist>