简体   繁体   English

AngularJS最佳实践 - 风格指南

[英]AngularJS best practices - Styleguide

I'm trying to use some of angulars best practices defined on the google-styleguide site: https://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html 我正在尝试使用google-styleguide网站上定义的一些角度最佳做法: https ://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html

But at the moment I'm struggling with some issues. 但目前我正在努力解决一些问题。 Before I used this styleguide I had the $scope variable available to do a $watch on a variable for instance. 在我使用这个样式指南之前,我有$scope变量可用于对变量进行$watch

app.controller('myController',['$scope', function($scope) {
    $scope.$watch('myVariable', function(val) {
       alert("I'm changed");
    });
}]);

Now with my new approach I don't know how to handle this? 现在用我的新方法我不知道如何处理这个问题? Should I still inject $scope ? 我还应该注入$scope吗? Because I do not have to inject $scope when I'm not using $watch . 因为当我不使用$watch时,我不必注入$scope

function myController($scope) {
   var vm = this;

   vm.myVariable = "aVariable";
   vm.$watch('vm.myVariable', function(val) {
      // error because $watch is undefined
   });

   //$scope.$watch - works
}

app.controller('myController',['$scope', myController]);

The styleguide also advices to make use of prototypes. 风格指南还建议使用原型。 But what if I had to inject a service? 但是,如果我必须注入服务怎么办? What is the best approach to use a service inside your prototype? 在原型中使用服务的最佳方法是什么?

function myController(MyService) {
   var vm = this;

   vm.myService = MyService;
}

myController.prototype.callService = function() {
   var vm = this;

   vm.myService.doSomething();
}

Is this correct? 这个对吗? Or am I missing something, is there a place where I can find more information about this style of angular programming? 或者我错过了什么,是否有一个地方可以找到关于这种角度编程风格的更多信息?

In my opinion it feels more like natural javascript and I want to use this way of organizing my AngularJS apps. 在我看来,感觉更像是自然的javascript,我想用这种方式来组织我的AngularJS应用程序。

Thanks in advance 提前致谢

Update 更新

For the 'service' problem I was thinking of something as follows: 对于“服务”问题,我想到的事情如下:

function MyBaseController(AService, BService, CService) {
   this.aService = AService;
   this.bService = BService;
   this.cService = CService;
}

function myController() {
   var vm = this;
   MyBaseController.apply(vm, arguments);
}

myController.prototype.doSomething() {
   var vm = this;
   this.aService.somethingElse();
}

But this doesn't feel right imo.. 但这感觉不对..

It is perfectly valid to inject $scope to get access to things like $watch even when your using the "controller as" syntax. 即使你使用“控制器为”语法,注入$ scope来访问诸如$ watch之类的东西也是完全有效的。 For example: 例如:

JS JS

var MyController = function($scope) {
    $scope.$watch('ctrl.someVar' function() {
        ...
    });

    this.someVar = 123;
}

MyController.$inject = ['$scope'];

HTML HTML

<div ng-controller="MyController as ctrl">
    ....
</div>

The first example you gave of injecting a service into a controller is good. 您给控制器注入服务的第一个例子很好。 For the inheritance example I would do something like this. 对于继承示例,我会做这样的事情。

var BaseController = function(AService, BService) {
    this.aService = AService;
    this.bService = BService;
}

BaseController.prototype.doSomethingWithAAndB = function() {
    ...
}

var MyController = function(AService, BService, CService) {
    BaseController.call(this, AService, BService);

    this.cService = CService;
}

MyController.$inject = ['AService', 'BService', 'CService'];

//Note: you'll need to add a polyfill for Object.create if you want to support ES3.
MyController.prototype = Object.create(BaseController.prototype);

If you find it is too cumbersome to specify all the parameters in the child controller you could always just inject $injector and pass that up to your base controller. 如果您发现在子控制器中指定所有参数太麻烦,您可以随时注入$injector并将其传递给您的基本控制器。

@rob answer is correct. @rob答案是对的。 There are perfectly valid reasons to still want $scope injected in your controllers, $watch $on $emit $broadcast to name a few. 有充分合理的理由仍然希望在您的控制器中注入$scope$watch $on $emit $broadcast仅举几例。 Sure you can get away with this requirement in some cases using directives or services, but it's not always worth the time or complexity. 当然,在某些情况下,您可以使用指令或服务来逃避此要求,但并不总是值得花时间或复杂性。

I've given the controller as syntax a go, but found that in most of my use cases I still had a dependency on $scope in my controllers. 我已经将controller as语法转发,但发现在我的大多数用例中,我仍然依赖于控制器中的$scope I disliked this and so I ended up with the following convention: 我不喜欢这个,所以我最终得到了以下约定:

var MyController = function($scope) {
  $scope.vm = {};
  var vm = $scope.vm;

  vm.propA = 1;
  vm.propB = 2;
  vm.funcA = function(){};

  $scope.$watch('vm.propA' function() {
  });
}

So it uses the 'old school' approach but with a new school feel. 所以它使用了“旧学校”的方法,但有了新的学校感觉。 Everything in the view hangs off vm . 在视图中任何事情都关闭vm

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

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