简体   繁体   中英

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. 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?

Usually, properties that should be used by HTML should be defined inside your controller, not service.

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.

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