Dynamic template in templatURL in angular2

前端 未结 5 1005
醉话见心
醉话见心 2020-11-30 06:52

I have been using ng-include in angular 1 whenever I had to include a tamplate dynamically in the page.

Now how to acheive this in angular 2. I have tried searching

5条回答
  •  执笔经年
    2020-11-30 07:33

    As @binariedMe accurately describes, ng-include is off in Angular 2 due to security considerations. The recommended method is to use a custom directive with slightly more programmatical overhead.

    Additionally, to prepare your Angular code for 2.0:

    myApp.directive('myInclude', function() {
        return {
            templateUrl: 'mytemplate.html'
        };
    });
    

    And rather than using ng-include on an element, simply add my-include:

提交回复
热议问题