Ilia Sidorenko
Ilia Sidorenko

Reputation: 2725

Using injected from controller in view in angular

Is there a way to use injected into controller modules in the view directly without binding it to controller explicitly? In other words, is there something along the lines of

<p>{{$ctrl.$injectedIntoThisController.$stateParams.name}}<p>

for when $stateParams is injected into controller?

Upvotes: 0

Views: 39

Answers (1)

Karim
Karim

Reputation: 8652

You cannot use a service without injecting it, you can use the $injector service to inject services dinamically based on a condition and then bind what you need to the view.

angular.controller('MyCtrl', ['$injector', function($injector){

 if(something){
  var service = $injector.get('MyService');
  this.prop = service.prop;
 }

}])

and in your view bind the property.

<div ng-controller="MyCtrl as $ctrl">
 <p>{{$ctrl.prop}}<p>
</div>

https://docs.angularjs.org/api/auto/service/$injector

Upvotes: 1

Related Questions