Skip to content

Instantly share code, notes, and snippets.

@sanketmaru
Created January 30, 2016 17:44
Show Gist options
  • Save sanketmaru/6025bcd69a9384e46d75 to your computer and use it in GitHub Desktop.
Save sanketmaru/6025bcd69a9384e46d75 to your computer and use it in GitHub Desktop.
Angular Isolated Scope
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-controller="MyAppController">
<outer twowayvar="twoWayBindingVar" demo-function="demoFunction()" inner-demo-function='innerDemoFunction()'>
</outer>
</body
</html>
<script id="jsbin-javascript">
angular.module('app', [])
.controller('MyAppController', ['$scope', function($scope) {
$scope.twoWayBindingVar = "this is a two way bind string";
$scope.oneWayBindVar = "this is a one way bind string";
// calling from directive
$scope.demoFunction = function() {
//alert("Inside demo function in controller");
};
$scope.innerDemoFunction = function(){
console.log("Inside inner demo function");
};
}])
.directive('outer', function() {
return {
restrict: "EA",
scope: {
twowayvar: "=",
innerDemoFunction: "&",
demoFunction: "&",
},
replace:true,
template: "<div><input type='text' ng-model='twowayvar'> </input> <inner inner-demo-function='innerDemoFunction()' twowayvar='twowayvar'></inner></div>",
link: function(scope, element, attrs) {
scope.demoFunction();
}
};
})
.directive('inner', function() {
return {
restrict: "EA",
scope: {
innerDemoFunction: "&",
twowayvar:"="
},
replace:true,
template: "<div><input type='text' ng-model='twowayvar'> </input> </div>",
link: function(scope, element, attrs) {
scope.innerDemoFunction();
}
};
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">angular.module('app', [])
.controller('MyAppController', ['$scope', function($scope) {
$scope.twoWayBindingVar = "this is a two way bind string";
$scope.oneWayBindVar = "this is a one way bind string";
// calling from directive
$scope.demoFunction = function() {
//alert("Inside demo function in controller");
};
$scope.innerDemoFunction = function(){
console.log("Inside inner demo function");
};
}])
.directive('outer', function() {
return {
restrict: "EA",
scope: {
twowayvar: "=",
innerDemoFunction: "&",
demoFunction: "&",
},
replace:true,
template: "<div><input type='text' ng-model='twowayvar'> </input> <inner inner-demo-function='innerDemoFunction()' twowayvar='twowayvar'></inner></div>",
link: function(scope, element, attrs) {
scope.demoFunction();
}
};
})
.directive('inner', function() {
return {
restrict: "EA",
scope: {
innerDemoFunction: "&",
twowayvar:"="
},
replace:true,
template: "<div><input type='text' ng-model='twowayvar'> </input> </div>",
link: function(scope, element, attrs) {
scope.innerDemoFunction();
}
};
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment