scope vs $scope

1,239 views
Skip to first unread message

Perry Poon

unread,
Aug 19, 2013, 11:06:54 AM8/19/13
to ang...@googlegroups.com
what's the difference between scope and $scope if scope is set to be isolated in link function of directives?

Sander Elias

unread,
Aug 19, 2013, 12:52:43 PM8/19/13
to ang...@googlegroups.com
Hi Perry,

Nothing but convention. There is no difference in the way you can use it. However, there is a difference in the way they get passed to the link function as opposed to the controllers. The controllers get their parameters through the dependency injection system, while link function get called with a fixed set off parameters, where the order of the parameters is fixed.

inside the link function or controller there is no difference whatsoever.

Regards
Sander Elias  

Perry Poon

unread,
Aug 19, 2013, 1:05:47 PM8/19/13
to ang...@googlegroups.com
Thanks for your reply;)

You mean scope and $scope are refereed to the same object if I declare

scope: {}

in the directive or different object? and which object they are refereed to?

在 2013年8月20日星期二UTC+8上午12时52分43秒,Sander Elias写道:

Jose Luis Rivas

unread,
Aug 19, 2013, 1:34:36 PM8/19/13
to ang...@googlegroups.com
On 8/19/13 12:35 PM, Perry Poon wrote:
> Thanks for your reply;)
>
> You mean scope and $scope are refereed to the same object if I declare
>
> scope: {}
>
> in the directive or different object? and which object they are refereed to?

You can inject $scope and use it as scope. I mean:

```
angular.controller('DummyCtrl', ['$scope', '$rootScope', function(scope,
INameItWhateverIWant) {
scope.prototypeFunction; // This is like using $scope.prototypeFunction
INameItWhateverIWant.h; // Like $rootScope.h
...
});
```


--
Jose Luis Rivas
http://joseluisrivas.net/

Sander Elias

unread,
Aug 19, 2013, 1:58:06 PM8/19/13
to ang...@googlegroups.com
Errr.....

No, in case of an isolated scope, those are different ones. They call it an isolated scope for a reason!
If you ask for an isolated scope, you get a fresh new shiny scope object. There is no link with the $scope that's outside the directive.
does that answer your question?

Regards
Sander


Perry Poon

unread,
Aug 19, 2013, 6:44:22 PM8/19/13
to ang...@googlegroups.com

So what is $scope in a directive which has a isolated scope?

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/-w6FAkpDYzM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Sander Elias

unread,
Aug 20, 2013, 12:18:06 AM8/20/13
to ang...@googlegroups.com
an undefined variable.

Sander Elias

unread,
Aug 20, 2013, 12:23:45 AM8/20/13
to ang...@googlegroups.com
Unless you renamed the default form.

if you do something like this, it will be undefined:

    app.directive("sample", function () {
        return {
            scope : {}, //isolated scope
            link : function (scope, iElement, iAttrs) {
                //in here $scope is undefined..
                //even if you don't choose to isolate your scope, it will be called 
                // scope in stead of $scope.
            }
        };
    });

regards
Sander

Perry Poon

unread,
Aug 20, 2013, 12:26:49 AM8/20/13
to ang...@googlegroups.com

Thanks, I think now I understand it.

Thanks again.

--

macha devendher

unread,
Apr 4, 2015, 1:48:30 AM4/4/15
to ang...@googlegroups.com
Thank you very much sander elias
Reply all
Reply to author
Forward
0 new messages