2017-09-21 42 views
0

我正在使用Angular JS進行簡單的網絡應用程序。將JSON字符串格式化爲段落

我有一個包含多個段落的JSON對象。因此我需要將這些段落分成兩行。我做了一些研究,並且發現了類似問題的一些答案,但是因爲它們沒有放在上下文中,所以我不瞭解它們。我嘗試着去查看JSON是否有內置的東西來強制換行,因爲這樣做,但我沒有找到。

非常感謝幫助!

HTML瓦特/角JS

<div class="bio"> 
    {{exhibits[whichItem].bio}} 
</div> 

JSON

[ 
    { 
    "name":"Name goes here", 
    "bio":"First long block of text goes here then it needs a break <br /> and the second long block of text is here." 
    } 
] 

AngularJS - controllers.js

var exhibitListControllers = angular.module('exhibitListControllers', ['ngAnimate']); 

exhibitListControllers.controller('ListController', ['$scope', '$http', function($scope, $http){ 
    $http.get('js/data.json').success(function(data) { 
    $scope.exhibits = data; 
    $scope.exhibitOrder = 'name'; 
}); 
}]); 

回答

1

https://docs.angularjs.org/api/ng/directive/ngBindHtml

https://docs.angularjs.org/api/ngSanitize

納克綁定-HTML計算表達式,並插入得到的HTML到以安全的方式的元件。默認情況下,生成的HTML內容將使用$ sanitize服務進行消毒。要使用這個功能,請確保$ sanitize可用,例如,通過在模塊的依賴項中包含ngSanitize(而不是在覈心AngularJS中)。爲了在你的模塊的依賴項中使用ngSanitize,你需要在你的應用程序中包含「angular-sanitize.js」。

在HTML頁面,您可以做到這一點

​​

,並在你的控制器:

angular.module('bindHtmlExample', ['ngSanitize']) 
.controller('ExampleController', ['$scope', function($scope) { 
$scope.myHTML = 
    'I am an <code>HTML</code>string with ' + 
    '<a href="#">links!</a> and other <em>stuff</em>'; 
}]); 

你也可以嘗試這樣的事情:

 

    app.filter('to_trusted', ['$sce', function($sce) { 
     return function(text) { 
     return $sce.trustAsHtml(text); 
     }; 
    }]); 

然後在v iew:

 

    ng-bind-html=" myHTML | to_trusted" 

+0

謝謝你一起發送!我試圖遵循這一點,雖然我認爲我理解它,但我正努力將其與當前的控制器合併。我將Angular JS - controllers.js添加到了我的問題中,以澄清當前的設置,並且找到了一些有關此答案的其他指導。 – user5854648

+0

在$ scope.myHTML中引用了一個字符串,但是這個字符串是JSON文件中JSON對象的一部分。我不知道如何合併它。 – user5854648

+0

我還沒有試過你的代碼,但我不認爲它可能很重要,它應該使用JSON或非JSON字符串。 –

相關問題