简体   繁体   English

通过调用角度控制器函数从ng-init指令初始化服务器数据?

[英]Initialize the server data from ng-init directive via calling angular controller function?

We have developed one Enterprise application, The complete client side framework is built on AngularJS and the server side framework is on ASP.NET Web API. 我们开发了一个企业应用程序,完整的客户端框架构建在AngularJS上,服务器端框架构建在ASP.NET Web API上。 In the application we have 350+ html pages and 250+ Web APIs. 在应用程序中,我们有350多个html页面和250多个Web API。 There are lots of areas where we initialize the server data from ng-init directive via calling controller function. 我们通过调用控制器函数从ng-init指令初始化服务器数据有很多方面。 Because in our application all data is coming from the ASP.NET Web APIs. 因为在我们的应用程序中,所有数据都来自ASP.NET Web API。 But I was surpised upon the documentation for ng-init, which says, in a nice bold outline : 但我对ng-init的文档感到惊讶,该文档以一个漂亮的大胆轮廓说:

The only appropriate use of ngInit for aliasing special properties of ngRepeat, as seen in the demo below. 唯一适当的用途是使用ngInpe来替换ngRepeat的特殊属性,如下面的演示所示。 Besides this case, you should use controllers rather than ngInit to initialize values on a scope. 除了这种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。

I would like to get feedback from Industry Experts/ Angularjs Core Team Members on my concerns. 我想得到行业专家/ Angularjs核心团队成员的反馈意见。

Let me brief first what we are doing in the application, for showing a industries list we load the industry HTML page and show the server data. 让我先简要介绍一下我们在应用程序中做了什么,为了显示行业列表,我们加载行业HTML页面并显示服务器数据。 The HTML template code is shown below: HTML模板代码如下所示:

<div ng-init="initIndustries()">....</div>

The "initIndustries" function will be called from initialized industryCtrl. 将从初始化的industryCtrl调用“initIndustries”函数。 industryCtrl.js code is shown below : industryCtrl.js代码如下所示:

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 few concerns based on Angularjs Doc and Anglarjs-styleguide guideline on Keep Controller Focused which is mentioned below : 基于Angularjs DocAnglarjs-styleguide关于保持控制器聚焦的指南,我几乎没有关注,如下所述:

  1. Are we doing something wrong to initialize the data via function because all data is coming from the Server APIs? 我们是否做错了通过函数初始化数据,因为所有数据都来自Server API?
  2. Current initialization approach is against the industry practices in AngularJS based application. 当前的初始化方法违反了基于AngularJS的应用程序中的行业惯例。 if yes, then which approach we need to follow in the application? 如果是,那么我们需要在申请中遵循哪种方法?
  3. Do we need to create sperate controller for every view just for the shake of client side test coverage, as per the angularjs-styleguide? 根据angularjs-styleguide,我们是否需要为每个视图创建sperate控制器,以便控制客户端测试覆盖范围?

The reason to implement the above approach(initialize the data from ng-init directive via controller function), Due to large application my initial thought was to follow MVC philosophy of decoupling the code and separation of concerns. 实现上述方法的原因(通过控制器函数从ng-init指令初始化数据),由于大量应用,我最初的想法是遵循MVC解耦代码和关注点分离的哲学。

Angular is known to be Model-View-Whatever. Angular被称为Model-View-Whatever。 So, it totally depends on you whether you follow a structure in the angular application or not. 因此,完全取决于您是否遵循角度应用中的结构。

Not following a structure has some pros and cons. 不遵循结构有一些优点和缺点。

Pros 1. A data that is available in one controller can be shared across many views so you won't have to use $rootScope or HTML5 localStorage/sessionStorage to pass data from one controller to another. 优点1.一个控制器中可用的数据可以在许多视图中共享,因此您不必使用$ rootScope或HTML5 localStorage / sessionStorage将数据从一个控制器传递到另一个控制器。

Cons 1. As data in controllers is available in multiple views so it is very difficult to maintain the data because the data is obviously bound to views and there will be many implicit angular watches on the same data. 缺点1.由于控制器中的数据在多个视图中可用,因此维护数据非常困难,因为数据显然绑定到视图,并且在同一数据上会有许多隐式角度监视。 That will give bad performance. 这将导致糟糕的表现。

Now answering your point 1 and 2 of using ng-init to intialize data in controller. 现在回答使用ng-init初始化控制器中数据的第1点和第2点。 I don't think that will be a good approach because you have made the initialization dependent on the view. 我认为这不是一个好方法,因为你已经根据视图进行了初始化。 Lets take a scenario in which your view is not found on request and then the initialization won't occur. 让我们看一个在请求时找不到您的视图的场景,然后不会进行初始化。 And as initialization won't occur, there will be garbage values / bugs in controller. 并且由于不会发生初始化,控制器中将存在垃圾值/错误。 So its better to separate the controller initialization from view. 因此最好将控制器初始化与视图分开。

Answer to point 3 is that when you are using a separate controller for every view, you are actually avoiding monolithic code approach and that is always a good point because that leads to modular approach. 对第3点的回答是,当您为每个视图使用单独的控制器时,实际上是在避免单片代码方法,这总是一个好点,因为这会导致模块化方法。 Separation of concerns in Angular JS is not just done using separate controller for every view but services and directives are also for this purpose in the long run. Angular JS中的关注点分离不仅仅是为每个视图使用单独的控制器,但从长远来看,服务和指令也是为了这个目的。

Hope that helps you in some way. 希望以某种方式帮助你。

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

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