Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting resolves into directive controllers

I'm using AngularUI router (0.2.13) and have a state defined as such

.state('foo', { 
    template:'<div some-directive></div>', 
    resolve: {
        foo: function(SomeService) {
            return SomeService.something().promise;
        }
    }
})

and a directive like this:

app.directive('someDirective', function(){
     return {
         controller: function(data) {
           // I want `data` to be injected from the resolve... 
           // as it would if this was a "standalone" controller
         }
     }
})

but this doesn't work - the data parameter causes an UnknownProvider error. Defining the directive controller independently and setting it by name in the directive has the same result.

I more or less get why this is happening, but have two questions:

  1. is there a way to do what I'm trying to do?
  2. should I be trying to do it, or have I slipped into an antipattern here?
like image 719
drew moore Avatar asked Mar 21 '15 01:03

drew moore


People also ask

What is directive controller?

Definition and Usage The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.

What is attrs in AngularJS?

scope is an AngularJS scope object. element is the jqLite-wrapped element that this directive matches. attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values. controller is the directive's required controller instance(s) or its own controller (if any).

How to write directive in AngularJS?

Create New Directives In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.

What is restrict in AngularJS directive?

Restrict. Angular allows us to set a property named restrict on the object we return on our directive definition. We can pass through a string with certain letters letting Angular know how our directive can be used. function MyDirective() { return { restrict: 'E', template: '<div>Hello world!


1 Answers

You can't use resolves in directives, but you can pass the result resolved in the state down to the directive which I think accomplishes what you're looking for.

You'd want to update your state definition to include a controller and set a parameter on the directive:

.state('foo', { 
   template:'Test<div some-directive something="foo"></div>',
   url: 'foo',
   resolve:{
       foo:function(SomeService){
           return SomeService.something();
       }
   },
   controller: function($scope, foo){
     $scope.foo = foo;
   }
})

Then update the directive to use this parameter:

.directive('someDirective', function(){
     return {
         controller: function($scope) {
           // I want `data` to be injected from the resolve... 
           // as it would if this was a "standalone" controller
           console.log('$scope.something: '+ $scope.something);
         },
         scope: {
           something: '='
         }
     };
})

Here's a sample plunker: http://plnkr.co/edit/TOPMLUXc7GhXTeYL0IFj?p=preview

like image 72
Brad Barber Avatar answered Nov 07 '22 22:11

Brad Barber