Skip to content

Instantly share code, notes, and snippets.

@jennysjottings
Created January 26, 2014 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennysjottings/8632132 to your computer and use it in GitHub Desktop.
Save jennysjottings/8632132 to your computer and use it in GitHub Desktop.
Example of binding isolated scopes in AngularJS
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<div my-directive
text="Hello {{ parentProperty2 }}!"
attribute-for-one-way-bind="parentProperty1"
attribute-for-two-way-bind="parentProperty2">
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.js"></script>
<script>
angular.module("myApp",[])
.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";
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment