AngularJS: Passing a reference to a controller function into a directive

// A directive with access to a function named ‘addressFunction(person)’ that takes a person object
// which exists in the CONTROLLER (*note that I don’t have to name the parameter ‘person’ when
// calling it so I’m doing ‘aperson’ just to prove that.
// you would load this with <search-result-3 person-object=”{{person}}” formatted-address-function=”addressFunction(aperson)”></search-result-3>

/*
<a href=”#” class=”list-group-item”>
<h4 class=”list-group-item-heading”>{{personObject.name}}</h4>
<p class=”list-group-item-text”>{{formattedAddressFunction(aperson: personObject)}}</p>
</a>
*/
myApp.directive(“searchResult3”,function(){
return{
templateUrl: ‘directives/searchResult.html’
, replace: true //will replace the original tag
//rather than putting this html inside existing tag
, scope:
{
personObject: “=” //tells us the directive expects a passed in value
// the = sign means this will be an object reference.
, formattedAddressFunction = “&” //a function is being passed in
}

}
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s