'AngularJS bind the entity to the HTML or bind controller dedicated data?
I am using AngularJS, and have an HTML with a form and a associated Controller. I have also a Employee entity class, a Department class and so on... So from the HTML I have this kind of lines for a form to add a new employee to the system:
<div class="col-sm-6">
<div class="form-group">
<label>Employee department</label>
<select class="selectpicker" ng-model="NewEmployeeController.employee.name">
<option
ng-repeat="department in NewEmployeeController.departments"
ng-value="department.getName()">{{department.getName()}}</option>
<option style="display:none" value=""></option>
</select>
</div>
</div>
In the AngularJS controller I have this:
var employeeModule= angular.module('employeeModule');
employeeModule.controller ("NewEmployeeController",function(){
var self = this;
self.employee = { name : null, department : {name : null, ...}, ...};
....
});
But I know I can just use the Employee entity class and have this line in the controller:
....
self.employee = new Employee();
....
instead this line:
....
self.employee = { name : null, department : {name : null, ...}, ...}; // or just self.employee; but I like to see in the controller all the attributes referenced by the html
....
Both ways work. The reason I have entity class is in client side I can have cohesive objects with responsability and not only data structures. But I dont like to have the html to bind and manipulate the entity directly (cause I dont want my entities to be in a invalid state, and because I think the html view of the entities and the entity are different concepts and could be different objects as the view could not require to know all the data of the entity).
But I dont like to declare lot of attributes in the controler (think in a large wizard form). I am not sure if its goog to write javascript classes for the view of the model (a Employee entity class and a NewEmployeeModelView class or something like this that the controller can bind to the HTML).
How is the usual way to address this in AngularJS framework?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
