angularjs - Injecting resolves into directive controllers -
i'm using angularui router (0.2.13) , have state defined such
.state('foo', { template:'<div some-directive></div>', resolve:{ foo:function(someservice){ return someservice.something().promise; } })
and directive this:
app.directive('somedirective', function(){ return { controller: function(data) { // want `data` injected resolve... // if "standalone" controller } } })
but doesn't work - data
parameter causes unknownprovider error. defining directive controller independently , setting name in directive has same result.
i more or less why happening, have 2 questions:
- is there way i'm trying do?
- should trying it, or have slipped antipattern here?
you can't use resolves in directives, can pass result resolved in state down directive think accomplishes you're looking for.
you'd want update state definition include controller , set parameter on 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 directive use parameter:
.directive('somedirective', function(){ return { controller: function($scope) { // want `data` injected resolve... // if "standalone" controller console.log('$scope.something: '+ $scope.something); }, scope: { something: '=' } }; })
here's sample plunker: http://plnkr.co/edit/topmluxc7ghxteyl0ifj?p=preview
Comments
Post a Comment