简体   繁体   English

如果角度视图直接访问服务,这是一个好习惯吗?

[英]If angular view accesses a service directly, is it a good practice?

I have injected a service to my Angular Controller and my View (HTML) accesses the service property directly. 我已将服务注入到Angular Controller并且我的视图(HTML)直接访问该服务属性。 The code looks like this: 代码如下:

angular.module('myApp', []);
angular.module('myApp')
   .controller('myCtrl', function($myServ) {
        this.myServ = $myServ;
   });

And in my View: 在我看来:

<div> {{ myCtrl.myServ.someProperty}} </div>

Is this a bad practice? 这是不好的做法吗? If I pass service property to my controller, then View accesses my controller property, is it better? 如果我将服务属性传递给控制器​​,则View会访问我的控制器属性,这样会更好吗?

Usually, properties that should be used by HTML should be defined inside your controller, not service. 通常,应由HTML使用的属性应在控制器内部定义,而不是在服务中定义。

Ideally, whatever is defined inside your service should be used by your service methods. 理想情况下,服务方法中应使用服务内部定义的任何内容。

So, if you are defining all your variables inside your service to keep your controller "thin", then yes, I think it is not good. 因此,如果要在服务中定义所有变量以使控制器保持“瘦身”,那么是的,我认为这不好。 Because, other developers after you will be looking for view variables inside your controller by default. 因为,默认情况下,您之后的其他开发人员将在控制器中寻找视图变量。

Hope it helps. 希望能帮助到你。

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

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