Insert HTML into view from AngularJS controller

前端 未结 18 1788
Happy的楠姐
Happy的楠姐 2020-11-21 06:00

Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?

This comes from a requirement to turn an

18条回答
  •  名媛妹妹
    2020-11-21 06:46

    Fortunately, you don't need any fancy filters or unsafe methods to avoid that error message. This is the complete implementation to properly output HTML markup in a view in the intended and safe way.

    The sanitize module must be included after Angular:

    
    
    

    Then, the module must be loaded:

    angular.module('app', [
      'ngSanitize'
    ]);
    

    This will allow you to include markup in a string from a controller, directive, etc:

    scope.message = "42 is the answer.";
    

    Finally, in a template, it must be output like so:

    Which will produce the expected output: 42 is the answer.

提交回复
热议问题