Skip to content

Instantly share code, notes, and snippets.

@cerebrl
Created July 15, 2013 01:14
Show Gist options
  • Save cerebrl/5996875 to your computer and use it in GitHub Desktop.
Save cerebrl/5996875 to your computer and use it in GitHub Desktop.
Angular Scope Linking in Directives

What do the little symbols for scope linking mean?

Well, say you have controller.js and view.html with the following code:

Your controller.js

$scope.name = 'world';

Your view.html

<div a="name" b="{{name}}">

Now, you have two different directives:

1) directive.js

// boilerplate directive stuff
scope: {
  name: '@'
}
// more boilderplate directive stuff

What are the resulting values of a and b?

  • '@' means copy the attr value.
    • "a" -> "name" (because that is what the value of the "a" attribute is)
    • "b" -> "world" (because it would be interpolated)

2) alt-directive.js

// boilerplate directive stuff
scope: {
  name: '='
}
// more boilerplate directive stuff

What are the resulting values of a and b?

  • "=" means evaluate the expression
    • "a" -> "world" (we are treating "name" as expression and it is "world")
    • "b" -> ERROR. Because "{{name}}" is not a valid expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment