Bind Angularjs to newly created html element dynamically

前端 未结 3 1690
心在旅途
心在旅途 2020-11-30 05:50

I have a tab page with multiple tabs that once clicked on call a service to return some data. Some of that data returns html forms and are very random. I want to collect t

3条回答
  •  旧巷少年郎
    2020-11-30 06:28

    Based on Sarah's answer, i created a structure to put the directive

    .directive('dynamic', function(AmazonService, $compile) {
      return {
        restrict: 'E',
        link: function(scope, element, attrs) {
          AmazonService.getHTML()
         .then(function(result){
           element.replaceWith($compile(result.data)(scope));
         })
         .catch(function(error){
           console.log(error);
         });
       }
     };
    });
    

    And in the html:

    
    

    Thanks Sarah, helped a lot!!!

提交回复
热议问题