AngularJS - Can I bind an object to an entire form? - angularjs

You'd think this is already part of Angular but I can't find it. You can bind form fields to variables in $scope using ng-model. But we're constantly binding, like, 100 fields exactly like this:
<form>
<input name="foo" ng-model="object.foo" />
<input name="foo2" ng-model="object.foo2" />
<!-- and so on ... -->
</form>
Does Angular provide a way to bind a form and have each field bind to it's corresponding object property? Something like this?
<form ng-model="object>
<input name="foo" />
<input name="foo2" />
<!-- and so on ... -->
</form>
You'd think something like that should exist, right? It violates DRY to individually bind by hand. It's also inflexible if the model changes.

As fals stated your approach seems messy.
I assume that you may want to repeat a model and dynamically create a form.
Using ng-form you may dynamically repeat a model bind ng-model on your $scope.
The pretty awesome part is that you may even have validation!
<div class="form-group" ng-repeat="human in people">
<ng-form name="customform{{$index}}">
<input type="text" id="email{{$index}}" ng-model="human.email" id="email{{$index}}" required>
</ng-from>
</div>
Demo

Related

Assigning AngularJS model inside ngRepeat

I would like to iterate over an array in AngularJS like this:
var languages = ['en', 'es', 'fr'];
To generate something like this:
<input type="text" ng-model="mymodel.en" placeholder="Name (EN)">
<input type="text" ng-model="mymodel.es" placeholder="Name (ES)">
<input type="text" ng-model="mymodel.fr" placeholder="Name (FR)">
I have tried this:
<div ng-repeat="language in languages">
<input type="text" ng-model="mymodel.{{language}}" placeholder="Name ({{language | uppercase}})">
</div>
But throws an error:
"Syntax Error: Token '{' is an unexpected token at column ..."
How should I perform this loop?
Use mymodel[language] instead of mymodel.{{language}}
<div ng-repeat="language in languages">
<input type="text" ng-model="mymodel[language]" placeholder="Name ({{language | uppercase}})">
</div>
plnkr
See the plnkr, and start typing in any of the input fields to see the model changes.
you cantry something like this,
<input type="text" ng-model="mymodel[language]" ...
You shouldn't use {{}} notation inside ng-model here you can refer to your property in the mymodel object.
But it seems that you are trying to get binding via model somehow for your inputs. It's not gonna work in your case because you need to init mg-model directives.
For catching changed in your model from dynamically generated you need to compile used directives using $compile service.
Here you can check out sample. I googled it but I've solved similar problem in similar way.

Why do I need to specify both a name and ng-model on input in AngularJS?

I was just wondering why I have to specify both name and ng-model on an input element in a form to be able to actually use it correctly.
For example, with the following:
<form name='test'>
<div>
<input type='radio'
value='create-new'
ng-model='toggle'
required />
<input type='radio'
value='use-existing'
ng-model='toggle'
required />
</div>
<div ng-switch='test.$invalid'>
<div ng-switch-when='true'>Invalid!</div>
<div ng-switch-when='false'>Valid!</div>
</div>
</form>
I would get the output Invalid! when I don't select a radio button - this is correct behaviour. However, the downside is that the only way I can access this model is through $scope.toggle - the element itself will not be referencable by name inside of $scope.test (the form container). $scope.test will contain the validation rules for toggle, but it will not have any way of letting you know that those validation rules are belonging to toggle as the name is not present.
If we were to switch it around put a name attribute on the input:
<form name='test'>
<div>
<input type='radio'
value='create-new'
name='toggle'
required />
<input type='radio'
value='use-existing'
name='toggle'
required />
</div>
<div ng-switch='test.$invalid'>
<div ng-switch-when='true'>Invalid!</div>
<div ng-switch-when='false'>Valid!</div>
</div>
</form>
Then our ng-switch at the bottom will always show Valid, even though I've mentioned that the input itself is required. In addition, toggle now shows up inside of $scope.test, so I can check the validity of $scope.test.toggle elsewhere (without requiring an inline attribute on that element).
If I combine both approaches and use both name and ng-model, then both results are combined and I get the result I would have expected - I can see $scope.test.toggle and the model itself is validating correctly.
My question is why is this the appropriate behaviour? It seems different to, say, jquery.validate.unobtrusive, where the name attribute is the bit that intrinsically ties the validation rules to the element.
name attribute is used for angular validation, ng-model for binding,
if you are not going to validate with angular you don’t have to use name
if you are not binding then don’t use ng-model
both are necessary if you need to validate in client side with angular and need binding

Detecting value change in ng-model without using $watch and form for application having large number of models

My application has a lot of models in the page. I want to detect whether user has changed value of any model on click of save. Using $watch on every model puts too much load, so don't want to use this method. Is there any good approach for this?
Small snippet is like below:
<div>
<div class="ttere2">
<input type="radio" name="nc2-radio3" ng-model="nc2PenaltyAfter" value="specificDays" />
<input class="ggfe1" ng-model="nc2AfterDays" ng-disabled="nc2PenaltyAfter!='specificDays'" type="number" min="1" max="100" step="1" value="1" />days</div>
<div class="admin_wp-line">
<input type="radio" name="nc2-radio3" ng-model="nc2PenaltyAfter" value="immediately"/> Immediately </div>
<div class="acfv1">model 1</div>
</div>
<div style="margin-top: 20px;"><button ng-click="saveData();">Done</button></div>
............too many inputs go here
</div>
Use .$dirty! Angular will set this on every element that is bound using ng-model, automatically, when it has been changed. It will also set it on the entire form. You can access it in code like this:
if ($scope.myForm.$dirty) {
// Your code here
}
Angular will provide six useful variables on the form, and every ngModel-bound element in your form: $dirty and $pristine, $valid and $invalid, and $touched and $untouched. You can mix and match these to drive a lot of useful behaviors, and they're available both in your controller (using the expression shown above) and your template (directly).

AngularJS Form is not in HTML

I am trying to use AngularJS Validation in order to validate a simple form, however I was having troubles getting my ng-class to show the correct class based off whether or not the input was dirty or not. Then when I looked at the actual HTML of the page, the <form> tags are not even in the document at all!
<form novalidate name="infoForm">
<p>To start, provide some basic information about the project.</p>
<ul class="ulFormGeneral">
<li>
<label>Company name</label>
<input id="CompanyName" ng-class="{ cvError : infoForm.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
</li>
</ul>
</form>
I want the cvError class to be added to this input if it is dirty, but nothing happens when I look at this in the browser. What am I doing wrong that is causing the <form> to just leave the DOM and then not work with my Angular expressions?
Welcome to the Angular world, no forms required! Here, the model is king. It looks like the problem is the ng-model and ng-class are point at different places.
Point everything at form.CompanyName (assuming that is the model name is form in the $scope):
<input id="CompanyName" ng-class="{ cvError : form.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
The ng-model binds to the $scope. When you change the input field, it is automatically updated in the $scope. No form is needed or hitting a submit button to get the data. The $scope is updated with each key stroke.
The controller should do the work of figuring out what to do with the changes in the model. For example, you can add an ng-click to a button that fires a function defined by the controller to save the model.

AngularJS: SetValidity in ng-repeat fields

I have a form that generates some fields using ng-repeat. I want to attach each independent field to set validity in case it is left empty. Is there a way to do this?
Thanks!
You need to use ng-form with your ng-repeat. I'd update your example, but you didn't give me one... so the idea is something like this:
<form name="myForm">
<div ng-repeat="item in items" ng-form="repeatForm">
<input type="text" name="foo" ng-model="item.foo" required/>
<span ng-show="repeatForm.foo.$error.required">required</span>
</div>
</form>
If you needed to access 'setValidity' on the model for some reason, you'd probably have to pass repeatForm.foo to a function either on your controller or on your scope. like ng-click="myWeirdValidationFunction(repeatForm.foo)". There's probably not much of a use case for this though.

Resources