-
-
Save abrjagad/bfa7b16c04deeab78bda to your computer and use it in GitHub Desktop.
isolated scopes.
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
// | |
angular.module("MyApp", []) | |
.controller("MathCtrl", function($scope) { | |
$scope.add = function(x, y) { | |
return x + y; | |
}; | |
}) | |
.directive("myAddThings", function() { | |
return { | |
restrict: "E", | |
template: "{{result}}", | |
scope: { | |
localFn: "&fn" | |
}, | |
link: function(scope) { | |
scope.result = scope.localFn({ | |
x: 1, | |
y: 2 | |
}); | |
} | |
}; | |
}); | |
scope.result = scope.localFn({ | |
x: 1, | |
y: 2 | |
}); | |
//See how here in the HTML you have... | |
<my-add-things fn="add(x, y)"></my-add-things> |
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
//http://jonathancreamer.com/working-with-all-the-different-kinds-of-scopes-in-angular/ | |
//= | |
angular.module("MyApp", []) | |
.controller("UserCtrl", function($scope) { | |
$scope.loggedInUser = { | |
name: "Austin Powers" | |
} | |
}) | |
.directive("myUserDirective", function() { | |
return { | |
restrict: "E", | |
template: "<input ng-model='user.name' /></div>", | |
scope: { | |
user: "=" | |
}, | |
link: function(scope) { | |
console.log(scope.user) // { name: "Austin Powers" } | |
} | |
}; | |
}); | |
//You would then use this directive like... | |
<my-user-directive user="loggedInUser"></my-user-directive> |
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
angular.module("MyApp", []) | |
.controller("UserCtrl", function($scope) { | |
$scope.loggedInUser = { | |
firstName: "Austin", | |
middleName: "Danger", | |
lastName: "Powers" | |
}; | |
}) | |
.directive("myUserDirective", function() { | |
return { | |
restrict: "E", | |
template: "{{fullName}}", | |
scope: { | |
fullName: "@name" | |
} | |
}; | |
}); | |
//And this is how you would use that directive... | |
<my-user-directive | |
name="{{loggedInUser.firstName}} {{loggedInUser.middleName}} {{loggedInUser.lastName}}"> | |
</my-user-directive> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment