AngularJS: how to have default value for select element? [duplicate] - html

This question already has answers here:
How to have a default option in Angular.js select box
(23 answers)
Closed 7 years ago.
I am almost new to AngularJS, and I am using a select tag for selection . What I want is to have a default value (i.e admin) for my select tag to define the role of my user which are agent and admin
Here is my HTML codes:
<div class="row">
<div class = "form-group col-xs-12 col-md-6">
<label>
Role
</label>
<select class="form-control" ng-options="value for value in roles" ng-model="newStaff.role_name='agent'"></select>
</div>
</div>
and here is my controller codes:
controllers.controller('StaffMembersNewCtrl', function ($scope, StaffMembers, $state) {
$scope.roles = ['admin', 'agent'];
StaffMembers.query(function(responce) {
$scope.staffs = responce;
});
$scope.SaveNewStaffMember = function(){
StaffMembers.save($scope.newStaff, function(){
$scope.addAlert('success', 'New staff member was added successfully!');
$state.go('staffMembersSettings');
});
};
});
It seems working, I have a default value, and data is bind, but there is an error in browser.
Error: [ngModel:nonassign] Expression 'newStaff.role_name='agent'' is non-assignable. Element: <select class="form-control ng-pristine ng-untouched ng-valid" ng-options="value for value in roles" ng-model="newStaff.role_name='agent'">
I understand this error, but I can not find an alternative solution for my problem which is putting a default value for my select tag.

You can define your default in JS or HTML. The way you're trying to use ng-model suggests you want to define the default in HTML. ng-init would be one way to do that (you're actually using ng-model the way one would use ng-init). Think of ng-model as a kind of "address" that points angular to WHERE you want to store the selection, not WHAT you want to store in there.
Since your data model is in JS, I suggest you do it there instead so that you can reference the appropriate role name in $scope.roles
HTML
<select class="form-control" ng-options="value for value in roles" ng-model="newStaff.role_name"></select>
CONTROLLER
$scope.newStaff = {role_name: $scope.roles[0]};

Try this..
<div class="row">
<div class = "form-group col-xs-12 col-md-6">
<label>
Role
</label>
<select class="form-control" ng-options="value for value in roles" ng-model="newStaff.role_name" ng-init="newStaff.role_name=roles[1]"></select>
</div>
</div>

Related

Can I change the current year of bsDatepicker?

In my typescript code I am just importing the => { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
I have this html codel:
<div class="col-xs-12 col-12 col-md-4 form-group">
<input type="text"
placeholder="Datepicker"
class="form-control"
bsDatepicker>
</div>
The result is:
what I have.png
I want the basic calendar to begging form 2004.
Something like: what I want.png
How can I change the first year that pops-out to be 2004 instead of 2020?
If there is no value set for the datepicker it will default to the current date. This is what you see happening in your example.
What you can do is inside the html template add the [bsValue]="dateForDatepicker" attribute to your bsDatepicker input element.
<input type="text"
placeholder="Datepicker"
class="form-control"
bsDatepicker
[bsValue]="dateForDatepicker"
>
In the component you can then add dateForDatepicker = new Date("01-01-2014") which should cause the datepicker to display that value initially.
For more information you can check the documentation: https://valor-software.com/ngx-bootstrap/#/datepicker#date-initial-state

Angular: How to programatically set select/select2 value after onSubmit()

I'm using HTML <select> Tag, so as it is known allready:
The element is used to create a drop-down list.
The tags inside the element define the available
options in the list.
I want to achieve next thing:
after I select value in a drop-down list I would like to set select's value to a default value which is "-/-" ..
Here is how it looks:
And code:
<div class="form-group">
<label class="control-label dash-control-label col-sm-3" for="">Select Select2:</label>
<div class="col-sm-3">
<select id="taxSelect" class="form-control dash-form-control select2" style="width: 100%;" data-minimum-results-for-search="Infinity">
<option disabled [ngValue]="null" >-/-</option>
<option *ngFor="let code of codes" [value]="code.id">
{{code.title}}
</option>
</select>
</div>
I mean when I open modal again I want to see "-/-" as it is default value, and not the value that I previously selected.. So basically onSubmit() in typescript I would like to reset to set again "-/-" as a default value..
P.S I've tried:
show() {
$('select').val('0');
$('select').val($('select').children().eq(0).val());
$('select2').val('0');
$('select2').val($('select').children().eq(0).val());
$("select[title='-/-']").attr('selected', 'selected');
}
So basically when Modal is shown I wanted to reset drop-down values to "-/-" but it doesn't work..
Thanks a lot guys!
CHEERS!

Getting hardcoded value from input tag into angular controller

I am using angularjs in my application.I am trying to pass hardcoded value from input tag into angularjs controller.Here i am not taking any dynamic values.User just clicks the input area.Based on clicked area i am passing value into angular controller.
Here is my html
<div class="first-hour">
<input type="text" value="12:00am - 12:30am" readonly>
</div>
<div class="second-hour">
<input type="text" value="12:30am - 01:00am" readonly>
</div>
If the user select first input text box value is 12:00am - 12:30am and if second means value is 12:30am - 01:00am.I need to get these values inside angular controller.Can anyone tell how to get the input hardcoded values directly into angularjs controller?
Here is an example of how you can select a specific filed. Both ranges have to be initialised in the Controller:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.range1 = "12:00am - 12:30am";
$scope.range2 = "12:30am - 01:00am";
$scope.select = function(val) {
$scope.display = angular.copy(val);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="first-hour">
<input type="text" ng-model="range1" ng-click="select(range1)" readonly>
</div>
<div class="second-hour">
<input type="text" ng-model="range2" ng-click="select(range2)" readonly>
</div>
Selected: {{display}}
</div>
(value was replaced with ng-model)
You can try like this,
create one object and initialize value in controller itself. then bind the object in html,
$scope.client = {};
$scope.client.start = "12:00am - 12:30am";
in html page,
<div class="first-hour">
<input type="text" ng-model="client.start" readonly>
</div>
please let me know if u have any quires.

AngularJs 1, input type having value to update dynamically

Hello guys i'm trying something out in angular and i'm struck in between, we can assign values to input element for eg, where parameters is a list of parameters and is in ng repeat
<input type="text" ng-model="parameters.inParameterName" class="form-control number" value="{{parameters.inParameterName = current}}"/>
^This works but when i try to get right hand side of value from backend ie in that parameter variable
<input type="text" ng-model="parameters.inParameterName" class="form-control number" value="{{parameters.inParameterName = parameters.formula}}"/>
the text box ng model is assigning to that string and i'm not able to edit the field
You just need to assign a value from your database to model parameter in controller file.
function TodoCtrl($scope) {
// just assign then value of textDemo what ever you like ie you can set from database value or static depends upon you.
$scope.textDemo = 'Testing';
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCtrl">
<input type="text" ng-model="textDemo" />
</div>
</div>

Perform angularjs operation inside value attribute of html input tag

I want to perform this operation but value attribute is not working
<input name="grand_total" type="text" class="form-control" value="#{{sum(saletemp)*discount1/100 | currency: "&#x9f3"}}" />
<input name="discount" type="text" class="form-control" id="add_payment" ng-model="discount1"/>
Second question
If I want to send value from a P tag after html form submission, how can I do that and how can I retrieve that value?
I want to pass this value #{{sum(saletemp)*discount1/100 | currency: "&#x9f3"}} to my controller and want to store it into my database. Here is the code portion
<div class="form-group">
<label for="grand_total" class="col-sm-4 control-label">Discount</label>
<div class="col-sm-8">
<p class="form-control-static" ><b>#{{sum(saletemp)*discount1/100 | currency: "&#x9f3"}}</b></p>
</div>
</div>
You should use ng-model instead of dealing with the value attribute of input tag... That will provide you more flexibility for doing almost any kind of operation over it and accessing it in controller..
like ng-model="saletemp" will be available to you in controller as $scope.saletemp.. and if you want to call functions on some change in input tag value, you can simply call ng-change="functionName()" which you will have defined in controller as
$scope.functionName = function(){YOUR FUNCTION DEFINITION}
Here is the fiddle which will allow you to put the currency in input tag when ng-model is used. http://jsfiddle.net/arunpjohny/wNBAn/