Last active
August 29, 2015 13:57
-
-
Save wulymammoth/9901464 to your computer and use it in GitHub Desktop.
Angular Isolate Scope with @
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head lang="en"> | |
| <meta charset="utf-8"> | |
| <title>Isolate Scope @</title> | |
| <style type="text/css"> | |
| .inline { | |
| display: inline-block; | |
| } | |
| </style> | |
| </head> | |
| <div ng-app="drinkApp"> | |
| <div ng-controller="AppController"> | |
| <input type="text" ng-model="ctrlFlavor" /> | |
| <div drink flavor="strawberry"></div> | |
| <!-- | |
| will break - no isolate scope | |
| both with be `peach` | |
| --> | |
| <div drink flavor="peach"></div> | |
| ctrlFlavor: <div drink flavor="{{ ctrlFlavor }}" class="inline"></div> | |
| </div> | |
| </div> | |
| <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.3/angular.min.js"></script> | |
| <script> | |
| var app = angular.module('drinkApp', []); | |
| app.controller('AppController', function($scope) { | |
| $scope.ctrlFlavor = 'blackberry'; | |
| }); | |
| app.directive('drink', function() { | |
| return { | |
| scope: { | |
| // the @ sets up so we can get flavor out of the attrs | |
| // automatically assigns to scope vs typing out verbose link fn | |
| flavor: '@' | |
| }, | |
| template: '<div>{{ flavor }}</div>', | |
| // link: function(scope, element, attrs) { | |
| // scope.flavor = attrs.flavor; | |
| // } | |
| }; | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment