Why isn't my $scope dependency resolved when I add it to the Angular Seed View1Ctrl

132 views Asked by At

I am experimenting with Angular Seed and I tried to add a $scope dependency to the View1Ctrl as follows:

 .controller('View1Ctrl', [function ($scope) {
        $scope.message = 'mundo';
    }]);

which does not work: $scope is undefined...

When I change the code to this:

.controller('View1Ctrl', function ($scope) {
        $scope.message = 'mundo';
    });

then $scope is resolved.

Can someone please explain why $scope is not resolved with the first snippet?

1

There are 1 answers

0
M21B8 On BEST ANSWER

if you use the [] you need to provide strings to match the dependency's to inject.

.controller('View1Ctrl', ['$scope', function ($scope) {
    $scope.message = 'mundo';
}]);

This is useful if you minify your javascript,

.controller('View1Ctrl', ['$scope', function (a) {
    a.message = 'mundo';
}]);

This means the $scope is injected as a variable named a. Without the [] it attempts to inject whatever the variable name is.