Difference between $ scope and scope in angularjs

I am new to angularjs. I would like to know what is the difference between $scope

in angularjs Controller and scope

in angularjs directive.

I tried to use scoped in my controller and I got the following error:

Error: [$ injector: unpr] Unknown provider: scopeProvider <- scope

+3


source to share


1 answer


$scope

is a service provided $scopeProvider

. You can inject it into controllers, directives or other services using Angular's built-in dependency injector:

module.controller(function($scope) {...})

which is shorthand for

module.controller(['$scope', function($scope) {...}])

In the first version, Injector Dependency specifies the provider name ("$ scopeProvider") based on the function parameter name ("$ scope" + "Provider"). The second version also creates a provider name like this but uses an explicit one '$scope'

in the array rather than the function parameter name. This means that you can use any parameter name instead $scope

.



Thus, you get code like this: module.controller(['$scope', function(scope) {...}])

where scope

can be anything, this is the name of the function parameter, can be foo

or a12342saa

.

The Dependency Injector basically does this:

function controller(def) {
    //def[def.length-1] is the actual controller function
    // everything before are it dependencies

    var dependencies = [];
    for(dep in def.slice(0, def.length-1)) {
         dependencies.push(__get_dependency_by_name(dep));
    }
    def[def.length-1].apply(dependencies);
}

      

I think the reason why using "scope" instead of "$ scope" as the dependency name does not work is now clear. There is no definition for "scopeProvider".

+4


source







All Articles