0

Is there a way to inject automatically a dependency (or a resolve) in the $scope without manually append it to $scope ? (with or without UI-Router)

Would the "controllerAs" syntax be of any help ?

.config(['$stateProvider',
    function($stateProvider) {

        $stateProvider
            .state('test', {
                url: '/test',
                templateUrl: 'test.tpl.html',
                controller: 'TestCtrl',
            })
        ;
    }
])
.controller('TestCtrl', ['$scope', 'myService',
    function($scope, $rootScope, accruals, myService) {
        $scope.myService= myService; // how can I avoid this every time ?
    }
])

Thanks

10
  • Why would you even want to do that? Why do you use $scope.myService= myService and not simply use myService directly? Commented Jul 24, 2014 at 8:55
  • Well .. my service handle the logic (that I don't want to put in a controller) and it's mainly my templates that call that service ... what's wrong with this ? Commented Jul 24, 2014 at 9:03
  • 2
    @Bixi Well, you shouldn't ;) Commented Jul 24, 2014 at 9:09
  • 1
    Ok that's interesting, could you elaborate on this ? You mean it's better practice to bind object/function/just-what-I-need to the $scope instead of the full service ? Commented Jul 24, 2014 at 9:11
  • 1
    @Bixi I do not want to say what is good or bad... but to solve your issue, you should/could use state nesting. If you assing to parent $scope something (method, object...) it will be available on a child $scope... and in fact that's how the ui-router was intended to work... profit from state and scope nesting... Maybe this could help stackoverflow.com/a/20558850/1679310 Commented Jul 24, 2014 at 9:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.