Angular JS Ng-model doesn't bind an input - html

I am facing a problem in Angular JS. Here is the code I wrote :
<script>
function diveInput($scope) {
$scope.dive_points = [ {depth: 0, date: 0}, {depth: 43, date: 1} ]
}
</script>
<div ng-app ng-controller="diveInput">
<ul>
<li ng-repeat="dive_point in dive_points">
<label>date :</label> <input type="number" min="0" ng-model="dive_point.date" />
<label>depth :</label> <input type="number" max="0" ng-model="dive_point.depth"/>
</li>
</ul>
<pre>{{dive_points}}</pre>
</div>
The expected behaviour is to see the two initials depth points in the inputs (0/0 and 1/43).
Then, if I change the value of an input, I except to see this modifications in the "" tag.
I did JSFiddle for this code if you want to play with it.
Why only the date of the second dive_point is displayed and not the depth ?
Why when I change one of the depth value, nothing works, the depth field disapear of the "pre" tag ?

You've got set max to 0 max="0" in your
<input type="number" max="0" ng-model="dive_point.depth" />
and in your controller you set depth to 43 so it was out of allowed range.
Please see working demo below
'use strict';
function diveInput($scope) {
$scope.dive_points = [{
depth: 0,
date: 0
}, {
depth: 43,
date: 1
}]
$scope.addDivePoint = function() {
$scope.dive_points.push({
depth: 0,
date: 0
});
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="diveInput">
<ul>
<li ng-repeat="dive_point in dive_points">
<div>
<label>date :</label>
<input type="number" min="0" ng-model="dive_point.date" />
</div>
<div>
<label>depth :</label>
<input type="number" max="50" ng-model="dive_point.depth" />
</div>
</li>
</ul>
<pre>{{dive_points}}</pre>
</div>

Related

Update range input in Vue.js 3

How can I update or show the actual value of a range input in Vue 3?
I thought it would be possible wiht v-model like this:
<input id="rangeSlider" type="range" class="form-range" v-model="value" min="0" max="5" step="0.2"/>
<label for="rangeSlider" class="sliderValue">
Value: {{ value }} m
</label>
It actually works exactly the same as the Vue2, just a little bit different Vue setup
const app = Vue.createApp({
data: () => ({
value: 0
})
})
const vm = app.mount('#app')
<script src="https://unpkg.com/vue#3.2.26/dist/vue.global.js"></script>
<div id="app">
<input id="rangeSlider" type="range" class="form-range" v-model="value" min="0" max="5" step="0.2"/>
<label for="rangeSlider" class="sliderValue">
Value: {{ value }} m
</label>
</div>

How can I set the value of input-group spinner to value of 1?

Honestly thought this was an easy issue to solve, but I'm stuck trying to figure this out. I want to set my input-group spinner to show the value 1 when the user opens the application.
<input id="option1" type="number" min="1" value ="1"
class="form-control"
ng-model="targetEntity.option1">
</div>
I thought value="1" would solve the issue but it hasn't shown the desired result, I was wondering what am I doing wrong?
Initialize the value of targetEntity.option1 in your controller:
angular.module('app', [])
.controller('ctrl', ($scope) => {
$scope.targetEntity = {
option1: 1
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<input id="option1" type="number" min="1"
class="form-control"
ng-model="targetEntity.option1" />
</div>

HTML input validation (Angular) dependent on other fields

I have two fields which both take numbers. One must always be higher than the other. For example you can have a field for age, and then a field for older sibling age which of course must be greater depending on the first field.
My fields are like this:
<input type="number" ng-model="age" required>
<input type="number" ng-model="olderSiblingAge" required>
I've tried using min="age" in the olderSibling input, but no luck, can still go below it.
You have to interpolate the value like this: min="{{vm.age}}" as specified in the number input documentation regarding to AngularJS.
angular
.module('app', [])
.controller('ctrl', function() {
var vm = this;
vm.age = 1;
vm.olderSiblingAge = 2;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl as vm">
<label> Age </label> <br />
<input type="number" ng-model="vm.age" required ng-change="vm.olderSiblingAge = vm.age">
<br /><br />
<label> Older sibling age </label> <br />
<input type="number" ng-model="vm.olderSiblingAge" required min="{{vm.age}}">
</div>
Note: I've used the controller-as syntax. Of course you can use it as your were doing it with the $scope notation like this min="{{age}}"
TRY USING THIS CODE
<input type="number" ng-model="age" required>
<input type="number" ng-change="olderSiblingAge = (olderSiblingAge < age)?age+1:olderSiblingAge" ng-model="olderSiblingAge" required>
To achieve expected result, use below option of using ng-blur to compare entered values
code sample https://codepen.io/nagasai/pen/XEJpYa?editors=1010
HTML:
<div ng-app="test" ng-controller="testCtrl">
Age <input type="number" ng-model="age" required>
Sibling Age <input type="number" ng-model="olderSiblingAge" required ng-blur="check()">{{olderSiblingAge}}
</div>
JS:
var app = angular.module('test',[]);
app.controller('testCtrl',function($scope){
$scope.check = function(){
if($scope.age > $scope.olderSiblingAge){
$scope.olderSiblingAge = null
alert("older sibling age should be greater than age")
}
}
})

checked the all checkboxes of form in angularjs

Hi i have form in angularjs and that form have many checkboxes on clicking first checkbox i,e check All i want to checked the all checkboxes . how i can i do that with angular for each field loop here is my sample code I am trying in code snipt.
vm.asas = function() {
angular.forEach($scope.quizSettingsForm, function(field) {
console.log(field);
angular.forEach(field, function(errorField) {
errorField.$setTouched();
});
});
console.log(vm.QuizSettings);
};
<form name="quizSettingsForm" novalidate>
<div class="panel panel-default">
<div class="panel-heading">
QUIZ SETTINGS
</div>
<div class="panel-body">
<ul>
<li>
<div class="checkbox">
<input type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="vm.asas()" />
<label for="checkall">Check /Uncheck ALL</label>
</div>
</li>
<li>
<div class="checkbox">
<input type="checkbox" ng-model="vm.QuizSettings.IsOpened" id="Cohort" />
<label for="Cohort">Have a open Quiz</label>
</div>
</li>
<li>
<div class="checkbox">
<input type="checkbox" ng-model="vm.QuizSettings.IsMandatory" id="mandatory" />
<label for="mandatory">Is Mandatory</label>
</div>
</li>
<li>
<div class="checkbox counterdiv">
<input type="checkbox" ng-model="vm.QuizSettings.ShouldExpireAfterExpiry" id="invitation" />
<label for="invitation"> <p>The quiz will expire <input type="number" ng-model="vm.QuizSettings.Expiry" ng-min="1" name="expiry"> days after invitation</p></label>
<span class="validation-message" ng-show=" vm.QuizSettings.ShouldExpireAfterExpiry && quizSettingsForm.expiry.$touched && quizSettingsForm.expiry.$error.min"><i class="fa fa-warning"></i> The minimum value is 1.</span>
</div>
</li>
<li>
<div class="checkbox">
<input type="checkbox" ng-model="vm.QuizSettings.AllowRetakes" id="dontallow" />
<label for="dontallow">Allow Retake and dont allow more than </label>
<input type="number" ng-model="vm.QuizSettings.AlllowMoreThen" ng-min="1" name="dontallow"><span style="color: #696969;font-size: 18px;font-weight: normal;"> Retakes </span>
<span class="validation-message" ng-show="quizSettingsForm.dontallow.$touched && quizSettingsForm.dontallow.$error.min"><i class="fa fa-warning"></i> The minimum value is 1.</span>
</div>
</li>
</ul>
</div>
</div>
</form>
One Approach (if checkbox properties are on same object with other properties)
You could (not necessarily should) store property names in an array, and then you could use a forEach loop to set the property itself on the vm.quizSettings object:
function selectAll(){
var myArray = [
"IsOpened",
"IsMandatory",
"ShouldExpireAfterExpiry"
];
angular.forEach(myArray, function(propName){
vm.QuizSettings[propName] = true;
});
}
Much Better (if checkbox properties are alone on same object)
It would be much better if you could avoid storing property names as strings. If all of your checkbox boolean properties to update were alone on it's own object, then it becomes much cleaner:
function selectAll(){
var myArray = Object.keys(vm.QuizSettings);
angular.forEach(myArray, function(propName){
vm.QuizSettings[propName] = true;
});
}
Better Still (simply decorate the checkboxes that this behavior should apply to)
Create a custom directive selectAllListener that listens to a "SelectAll" event and updates ngModel value to true when that event is raised.
function thisDirective() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, el, attr, ngModel){
scope.$on("SelectAll", function(){
ngModel.$setViewValue(true);
ngModel.$render();
});
}
}
}
Add the select-all-listener decorator directive to your checkboxes:
<li>
<div class="checkbox" >
<input select-all-listener type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="vm.asas()"/>
<label for="checkall" >Check /Uncheck ALL</label>
</div>
</li>
Raise the event from within your controller:
function selectAll(){
$rootScope.$broadcast("SelectAll");
}
Build an object for your form where isChecked key refers to checkbox value and name refers the label and value refers the model or value of input.
$scope.FormAttributeList = [
{
id: 1,
name: "xyz",
value: "xyz",
isChecked: false
},
{
id: 2,
name: "xyz",
value: "xyz",
isChecked: false
},
{
id: 3,
name: "xyz",
value: "xyz",
isChecked: false
},
{
id: 4,
name: "xyz",
value: "xyz",
isChecked: false
}
]
your code will be like
<div class="panel-body">
<ul>
<li>
<div class="checkbox" >
<input type="checkbox" id="checkall" ng-model="vm.checkAll" ng-click="selectAll(vm.checkAll,FormAttributeList)"/>
<label for="checkall" >Check /Uncheck ALL</label>
</div>
</li>
<li ng-repeat="item in FormAttributeList">
<div class="checkbox">
<input type="checkbox" ng-model="item.value" id="item.id" />
<label for="Cohort" >{{item.name}}</label>
</div>
</li>
</div>
Now create a function like
$scope.selectAll = function(checkAll,List) {
angular.forEach(list,function(value,key){
value.isChecked = checkAll;
});
};
This is the best way to do what you want it'll remove your unnecessary code.

AngularJS Form - Default Value for Select

I'm trying to create an angularjs form with two fields, one text input and a select. By default, the text input is blank but the select should have a value (the first element on the select). I'm following the angularjs form docs and using a master object which on reset is copied into the page's model. Here's the thing, if I set the select to something by default it comes blank. If I do the same with the text field it displays the default value. Below you'll find my code and here's a plunker. I think it might have to do with the copy being made on the reset function but I'm printing the object to the console at that point and it looks like it's being copied. If I try to do this without the master stuff, just setting it straight to the user and not calling reset it works ok.
controller
(function(angular) {
'use strict';
angular.module('formExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.master = {};
$scope.businessUnits = [{"businessUnitId":1,"name":"Auto"},{"businessUnitId":2,"name":"Life"},{"businessUnitId":3,"name":"Health"},{"businessUnitId":4,"name":"Surety"},{"businessUnitId":5,"name":"Finance"},{"businessUnitId":6,"name":"Dwelling"}];
$scope.master.businessUnit = $scope.businessUnits[0];
$scope.master.name = "John";
$scope.update = function(user) {
$scope.master = angular.copy(user);
};
$scope.reset = function(form) {
if (form) {
form.$setPristine();
form.$setUntouched();
}
$scope.user = angular.copy($scope.master);
console.debug($scope.user.businessUnit);
};
$scope.reset();
}]);
})(window.angular);
html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example33-production</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="formExample">
<div ng-controller="ExampleController">
<form name="form" class="css-form" novalidate>
Name:
<input type="text" ng-model="user.name" name="uName" required="" />
<br />
<div ng-show="form.$submitted || form.uName.$touched">
<div ng-show="form.uName.$error.required">Tell us your name.</div>
</div>
E-mail:
<input type="email" ng-model="user.email" name="uEmail" required="" />
<br />
<div ng-show="form.$submitted || form.uEmail.$touched">
<span ng-show="form.uEmail.$error.required">Tell us your email.</span>
<span ng-show="form.uEmail.$error.email">This is not a valid email.</span>
</div>
<select class="form-control" ng-model="user.businessUnit" ng-options="unit as unit.name for unit in businessUnits" required></select>
Gender:
<input type="radio" ng-model="user.gender" value="male" />male
<input type="radio" ng-model="user.gender" value="female" />female
<br />
<input type="checkbox" ng-model="user.agree" name="userAgree" required="" />
I agree:
<input ng-show="user.agree" type="text" ng-model="user.agreeSign" required="" />
<br />
<div ng-show="form.$submitted || form.userAgree.$touched">
<div ng-show="!user.agree || !user.agreeSign">Please agree and sign.</div>
</div>
<input type="button" ng-click="reset(form)" value="Reset" />
<input type="submit" ng-click="update(user)" value="Save" />
</form>
</div>
</body>
</html>
Add the following code to your <select>
ng-init="user.businessUnit = businessUnits[0]"
It should look like this:
<select class="form-control" ng-model="user.businessUnit" ng-init="user.businessUnit = businessUnits[0]" ng-options="unit as unit.name for unit in businessUnits" required></select>