Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of binding isolated scopes in AngularJS
<!DOCTYPE html>
<body ng-app="myApp">
<div ng-controller="myController">
<div my-directive
text="Hello {{ parentProperty2 }}!"
<script src=""></script>
.directive("myDirective", function () {
return {
restrict: "A",
scope: {
text: "@", // Attribute name is optional if attribute has same
oneWayBoundProperty: "&attributeForOneWayBind", // name as property on isolated scope. (If you do
twoWayBoundProperty: "=attributeForTwoWayBind" // specify the name, remember to camelCase it.)
link: function (scope) {
var temp = scope.oneWayBoundProperty(); // A one-way bound property has to be called as a function
scope.twoWayBoundProperty = "Jenny"; // This will also change parentProperty2
template: 'My text is "{{ text }}" <br> My oneWayBoundProperty is "{{ oneWayBoundProperty() }}" <br> My twoWayBoundProperty is "{{ twoWayBoundProperty }}"'
}).controller("myController", function ($scope) {
$scope.parentProperty1 = "World";
$scope.parentProperty2 = "User";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment