2017-04-21 84 views
0

我需要在窗體中打印此數組。但它應該自動生成而不用寫入html文件。我試過但我沒有得到結果。如何在Angularjs中的表單內打印數組值?

這是打印值並生成結果(需要在.js文件中編寫代碼)。

result+='<form action="http://www.example123.com" method="post" onsubmit="target_popup(this)">'; 
    for(component in components){ 
    result+= mylogic(component,components); 
    } 
    "<div> <button >Submit</button> </div></form>"; 


    $scope.target_popup=function (form) { 
    window.open('', 'formpopup', 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,width=400,height=400,resizeable,scrollbars'); 

    } 


}) 

<html> 
<div ng-bind-html="mylogic(result) | unsafe"></div> 
</html> 

回答

1

可以使用$sce.trustAsHtml(html)從JS

創建過濾器這樣

.filter('trust',function($sce){ 
    return function(html){ 
    return $sce.trustAsHtml(html) 
    } 
}) 

調用它的HTML

<div ng-bind-html="result | trust"> 

    </div> 

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
$scope.result ='<form action="http://www.example123.com" method="post" onsubmit="target_popup(this)"><div> <button >Submit</button> </div></form>'; 
 

 
}) 
 
.filter('trust',function($sce){ 
 
    return function(html){ 
 
    return $sce.trustAsHtml(html) 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<div ng-bind-html="result | trust"> 
 
    
 
</div> 
 
</div>
渲染HTML

+0

: - 你會幫助我嗎? https://codepen.io/anon/pen/ybOjqv –

+0

:-i試圖以我需要的方式調用我的動態json輸入,但不工作 –

+0

模塊和基本組件在codepen中缺失,請創建一個有效的演示 –