1

I have been thinking this question for a while, what's best practice to reuse html template in angularjs given the view(html) is bound to controller in case of two-way data binding ?

for example:

<a ng-repeat="(uuid, parent) in pageData.parents" class="list-group-item list-group-item-padding"  ng-click="click(parent.uuid)">
    <table class="table-grid">
        <tbody>
        <tr>
            <td>
                {{parent.name}}
            </td>
            <td>
                <span class="pull-right {{parent.stateCss}} state-label-font">{{parent.state}}</span>
            </td>
        </tr>
        </tbody>
    </table>
</a>

in above snip of code, the view is bound to some controller let's say ParentController, you can see all data in view are named after 'parent'. Now I have another controller say ChildController which wants to reuse the same html piece of code but the data are named after 'child'. That says data binding in this example will change to {{child.name}}, {{child.stateCss}} ...

the way Angularjs binds model to view kind of causes a lot of duplicate html code even they look quite similar. Actually not Angularjs, server side MVC framework has the same problem as long as you hard code model variable name in view file for later rendering.

I saw some web application not using MVC has a few html code which setup basic skeleton of page, then uses JQuery to dynamically feed data to view in programming way. This JQuery way is not my preference but it does save lots of html code, a application I think is fairly complex only as ~1000 html code that accomplishes tasks like ~10 sub pages, list, wizards ...

In angularjs, one solution I can imagine is replacing model variables in view with customized directive to change their scope/controller at runtime. Or having a directive receiving a configuration object which generates html element binding to configured controller/scope in compiling time.

what's your thought?

2 Answers 2

2

The Angular UI Bootstrap project accomplishes this using a combination of directives and the $templateCache service. Essentially, $templateCache is prepopulated with the HTML template and given a static url like template/somedirective/snippetname.html. Then, the directive specifies that url as its templateUrl. Take a look at the Writing Directives (Long Version) section of the guide, or even better, look at some source code!

Sign up to request clarification or add additional context in comments.

1 Comment

yes. Angular UI is an example of reuse, the dialog is a good example. it seems to make reuse is a advanced topic which is not easy to master
0

I think for reuse of html the proper solution is to make custom directives. They are a little difficult at first, but you can gain a lot of productivity with them.

Comments

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.