1

I have some repetitive components on my AngularJS page such as billingAddress, shippingAddress and primaryAddress. I have created a separate template file for address components and expected to be able to use ng-include to include the template thrice on my page. I am unable to find documentation around passing models to templates. What I am looking for is something like

<div ng-include="address.tpl.html" ng-model="{address: primaryAddress}"></div>
<div ng-include="address.tpl.html" ng-model="{address: billingAddress}"></div>
<div ng-include="address.tpl.html" ng-model="{address: shippingAddress}"></div>

Is this even possible as of now?

1
  • 1
    why not to make it as a directive? Commented Jun 2, 2014 at 15:48

1 Answer 1

1

his is for what directives are made.

angular.module('docsSimpleDirective', [])
  .directive('myAddress', function() {
    return {
      scope: {
          address : '='
      },
      templateUrl: 'address.tpl.html'
    };
  });

Then in your template simple use the $scope.address. On declaring the directive you should use it like this.

<my-address address="primaryAddress"><my-address>
<my-address address="billingAddress"><my-address>
<my-address address="shippingAddress"><my-address>
Sign up to request clarification or add additional context in comments.

2 Comments

I still think AngularJS should make it easier to pass in models to ngInclude's scope. The best practices lists tell me that directives are meant to be used when you have some DOM manipulation that you can't get away. But, I think directive is the only option I have at the moment.
Directives are usefull when you need to reuse some code that involves HTML. That is exacly what you needed here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.