Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichalZalecki/3ed6d99640379b8e064e to your computer and use it in GitHub Desktop.
Save MichalZalecki/3ed6d99640379b8e064e to your computer and use it in GitHub Desktop.
AngularJS: Isolated scope and function parameter
<!DOCTYPE html>
<html ng-app="myApp" ng-controller="myApp.myCtrl">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.js"></script>
<meta charset="utf-8">
<title>AngularJS: Isolated scope and function parameter</title>
</head>
<body>
<h1>AngularJS: Isolated scope and function parameter</h1>
<a href="http://jsbin.com/qeqezaqaxo/4/edit?html,js,console">see at jsBin</a>
<p>Look at the <strong>console</strong>!</p>
<car passengers="people" with-passenger="greet"></car>
<bus passengers="people" with-passenger="greet2(name)"></bus>
</body>
</html>
angular.module 'myApp', []
.controller 'myApp.myCtrl', ['$scope', ($scope) ->
$scope.people = [
'Michal',
'Piotr',
'Ala'
]
$scope.greet = (name) ->
console.log "Hi #{name}!"
$scope.greet2 = (name) ->
console.log "Hello #{name}!"
]
# passing function without invoking
.directive 'car', ->
restrict: 'E'
scope: {
passengers: '='
withPassenger: '&'
}
link: (scope) ->
scope.withPassenger()(person) for person in scope.passengers
# passing object
.directive 'bus', ->
restrict: 'E'
scope: {
passengers: '='
withPassenger: '&'
}
link: (scope) ->
scope.withPassenger({name: person}) for person in scope.passengers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment