How to make AngularJS compile the code generated by directive?

前端 未结 3 954
春和景丽
春和景丽 2020-12-30 12:25

Please help me on, How can we make AngularJS compile the code generated by directive ?

You can even find the same code here, http://jsbin.com/obuqip/4/edit

<

3条回答
  •  孤独总比滥情好
    2020-12-30 12:28

    Here's a version that doesn't use a compile function nor a link function:

    myApp.directive('helloWorld', function () {
      return {
        restrict: 'E',
        replace: true,
        scope: {
          myUsername: '@'
        },
        template: '
    Hello {{myUsername}}
    ' + '
    Sorry, No user to greet!
    ', }; });

    Note that the template is wrapped in a because a template needs to have one root element. (Without the , it would have two

    root elements.)

    The HTML needs to be modified slightly, to interpolate:

    
    

    Fiddle.

提交回复
热议问题