initialize the data from ng-init directive via controller function? is this the best practice?

330 views
Skip to first unread message

Ajay

unread,
Aug 10, 2016, 2:23:09 PM8/10/16
to AngularJS
I have read in the ngInit docs that says:  

The only appropriate use of ngInit for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.


We have developed one Enterprise application, the complete client side framework is built on AngularJS and the server framework is on ASP.NET Web API. In the application we have 350+ html pages and 250+ Web APIs. There are lots of areas where we initialize the data from ng-init directive via controller function. Because in our application all data is coming from the ASP.NET Web APIs.  Please check below code example :

HTML template code to initialize the industry data and bind into the grid:
<div ng-init="initIndustries()">....</div>

Industry Controller
app.controller("industryCtrl",["$scope",function($scope){

//Sets the server data in the $scope property.
$scope
.initIndustries = function(){
//perform ajax request and set the data into the $scope property.
}

$scope
.initAddIndustry = function(){
// perform ajax request for getting a dropdown data on Add Industry Page.
}
}])

I have two concern after reading the ngInit Doc and code example.

1. Are we doing something wrong to initialize the data via function because all data is coming from the Server APIs? 
2. Current initialization approach is against the standard practices in AngularJS based application. if yes, then which approach we need to follow in the application? 

the reason to implement the above approach, Due to large application my initial thought was to follow MVC philosophy of decoupling the code and separation of concerns. 

If we follow the Angularjs ngInit Doc Example, as per my assumption we need to create four controller for performing CRUD operation on single entity(Industry). say for example I want to load the industry data on the page and also perform Create, Update and Delete operation on a different HTML templates. 
as per the above scenario, Do I need to create four controller to achieve this functionality or Is there any other way of doing? 




Sander Elias

unread,
Aug 10, 2016, 10:43:33 PM8/10/16
to AngularJS
Hi Ayay,

To start of I would say, read through the styleguide, as it will answer a lot of your questions.

But aside from that, in your use-case there is no difference from calling your initIndustries from within your controller. Also you can have 1 controller handling all CRUD variants, and still follow guidelines.


Regards
Sander

Ajay

unread,
Aug 11, 2016, 1:45:21 AM8/11/16
to AngularJS
Hi Sander,

Thanks for the response.

I read the doc. The same thing is mentioned over there in the title of Keep Controller Focused. As per the AngularJS style guide we need to create 350+ client controllers for every view because we have 350+ HTML Templates. Just for the shake of client side test coverage. 

Is this is the right approach?

I didn't understand your suggestion "in your use-case there is no difference from calling your initIndustries from within your controller. Also you can have 1 controller handling all CRUD variants". It will be good if you describe in detail.

Regards,

Ajay Ojha

Sander Elias

unread,
Aug 11, 2016, 2:27:09 AM8/11/16
to AngularJS
Hi Ajay,

A view !== html template. A view can call in different templates as needed. So, no, you don't need to create 350+ controllers. You do need some logic to handle the differences.

Regards
Sander

Ajay

unread,
Aug 11, 2016, 3:16:41 AM8/11/16
to AngularJS
Hi Sander,

1.View === ng-view 

Ajay

unread,
Aug 11, 2016, 3:29:03 AM8/11/16
to AngularJS

Hi Sander,

view === ng-view or view === readable code in controller ?.

My concerns are mentioned above. It will be good, put your comments on this based on your experience or can you please suggest where I need log this ticket for getting right answer. Because I cannot relate your answer to my questions which i have asked.

Regards,

Ajay Ojha

Ajay

unread,
Aug 12, 2016, 3:35:52 AM8/12/16
to AngularJS
It will be good, if any share their experience / suggestions.

Regards,

Ajay Ojha.
Reply all
Reply to author
Forward
0 new messages