简体   繁体   English

当我们在一个页面中放置多个ng-init(多个部分页面的组合)时,会出现任何问题吗

[英]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. 这三个partial views都具有自己的加载数据列表的方法。

These methods to load list of data is called on ng-init so will there be any issue? 这些加载数据列表的方法是在ng-init上调用的,所以会出现问题吗?

The code of ng-init in all the partial views looks like as below: 在所有partial viewsng-init的代码如下所示:

<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. ng-init =“ ctrl.GetStudentDetails()”我猜想会获得针对Student_details部分视图的学生详细信息列表。 If you have a GetStudentMarks() and a GetStudentAttendance() and neither of the methods share the same collection then you are good to go. 如果您有一个GetStudentMarks()和一个GetStudentAttendance(),并且这两个方法都没有共享相同的集合,那么您就很好了。

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: 如果出于某种奇怪的原因,您正在使用GetStudentDetails()来向集合中填充有关详细信息,分数和出勤率的信息,则它将如下所示:

  • ng-init="ctrl.GetStudentDetails()" is called on the details div 在详细信息div上调用ng-init =“ ctrl.GetStudentDetails()”
  • ng-init="ctrl.GetStudentDetails()" is called on the marks div 在标记div上调用ng-init =“ ctrl.GetStudentDetails()”
    replacing the results for the above collection 替换以上集合的结果
  • ng-init="ctrl.GetStudentDetails()" is called on the attendance div replacing the results of the collection above. 在考勤div上调用ng-init =“ ctrl.GetStudentDetails()”替换上面集合的结果。

Obviously you want 3 different sets of results. 显然,您需要3组不同的结果。 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. 如果您有一个站点,需要列出学生的详细信息,然后对学生运行一些报告,则有必要将学生和报告的控制器分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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