| 1. |
What is the difference between the scopes of a directive and the scopes of a controller? |
|
Answer» Scopes of the controller and the directives are the instances of scope objects. The only difference lies in the naming convention for them. To understand the difference between scope and $scope, we NEED to understand directives with isolated scope using the following code: app.directive('testAppDirective', function() { return { scope: {}, link: function(myScopeVariable, elem,attr) { console.log(scope); } }});Here, we have defined a directive with isolated scope. The link function takes the signature scope, element, and attribute as the parameters. The name of the signature scope parameter can be anything as that parameter will be tagged to the scope of the directive’s object. The $scope object that is USUALLY injected into the controller cannot be used with another name. For example, app.controller(‘myTestController’,function(newScope){});results in error Error: [$injector:unpr] Unknown provider: scopeProvider <- scope <- myTestControllerBecause the AngularJS dependency SYSTEM tries to locate the dependency of the name newScope but fails to FIND it. Hence, to mark the dependency appropriately, the input to the controller function should be $scope. |
|