Can locals be injected into a controller set with ng-controller?

后端 未结 1 1227
小鲜肉
小鲜肉 2021-02-10 00:51

Referring to the example below, is there a way to use myCtrl instead of myCtrl2, passing an argument as a local instead of attached to $scope?

The $controller service pe

1条回答
  •  温柔的废话
    2021-02-10 01:15

    I can't quite tell from your question what exatly you're looking for, but you might try creating your own directive (a modified version of the ngController directive) can specify controller injectables:

    app.directive('myController', function($controller) {
      return {
        scope: true,
        link: function(scope, elem, attrs) {
          var locals = scope.$eval(attrs.locals);
          angular.extend(locals, {$scope: scope});
          $controller(attrs.myController, locals);
        }
      };
    });
    

    You would use it something like this:

    Here's a JsFiddle that demonstrates the technique: http://jsfiddle.net/BinaryMuse/qBZZk/

    0 讨论(0)
提交回复
热议问题