简体   繁体   中英

Will there by any problem when we put more than one `ng-init` in a single page (combination of multiple partial page)

There are more than one partial views in the page. For example.

1. index.cshtml is the main page.
2. _student_details.cshtml is partial page.
3. _student_marks.cshtml is partial page.
4. _student_attendance.cshtml is partial page.

All the three partial views has their own methods to load the list of data.

These methods to load list of data is called on ng-init so will there be any issue?

The code of ng-init in all the partial views looks like as below:

<div id="student-list" ng-controller="StudentController as ctlr"
     ng-init="ctrl.GetStudentDetails()" ng-cloak>

If your page is only dealing with Student information and each uses a different collection to get what it needs then it will be fine. ng-init="ctrl.GetStudentDetails()" I am guessing gets a list of student details for student_details partial view. If you have a GetStudentMarks() and a GetStudentAttendance() and neither of the methods share the same collection then you are good to go.

If for some weird reason you are using GetStudentDetails() to fill a collection with the information for details, marks and attendendance then it will go like this:

  • ng-init="ctrl.GetStudentDetails()" is called on the details div
  • ng-init="ctrl.GetStudentDetails()" is called on the marks div
    replacing the results for the above collection
  • ng-init="ctrl.GetStudentDetails()" is called on the attendance div replacing the results of the collection above.

Obviously you want 3 different sets of results. So if you do want to go with this weird approach you would require a different controller for details, marks and attendance. I only say this is weird in this case as you are only dealing with one responsibility which is Students so you definitely do not want to go with this approach but thought I would explain it anyways. If you had a site which you needed to list student details and then run some reports on students it would then make sense to separate your controllers for students and reporting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM