4

I have a JSON file generated via php:

$JsonObjItems = json_encode($arrObjItems, JSON_PRETTY_PRINT);
$objFile = new \File('test.json', true);
$objFile->write($JsonObjItems);
$objFile->close();

JSON:

[
    {
        "raw": {
            "id": "2",
            "pid": "0",
            "sorting": "0",
            "tstamp": "1433706234",
            "Alias": "-2",
            "Menu1": "Test (A,C,F)\nTest1, Gr\u00fcn",
            "Menu2": "Test1",
            "From": "1433714400",
            "To": "1434060000",
            "Published": "1"
        },
        "text": {
            "Alias": "-2",
            "Menu1": "Test (A,C,F)\nTest1, Gr\u00fcn",
            "Menu2": "Test1",
            "From": "08.06.2015",
            "To": "12.06.2015",
            "Published": "1"
        },...

I'm parsing that within an angularjs/ionic app:

.controller('GetJson', function ($scope, $http) {

    $http.get("test.json")
        .success(function (data) {
        $scope.all = data;    
        $scope.menu1 = data[0].text.Menu1;  
    });   
})

And put it out here:

<ion-view view-title="test" ng-controller="GetJson">
<ion-content class="padding">
    {{menu1}}
</ion-content>
</ion-view>

That generates this output:

Test &#40;A,C,F&#41;\nTest1, Gr\u00fcn

What I want is:

Test (A,C,F)
Test1, 
Grün

How can I do that? Within the javascript json encode or within the php generating the json? I tried that stuff in the php but I didn't got it working:

$JsonObjItems = json_encode($arrObjItems, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);

EDIT:

With the "$sce.trustAsHtml" Filter it works here: https://jsfiddle.net/uPw2U/163/ But not with expressions: https://jsfiddle.net/gstcppgt/3/

In both cases the \n line break is ignored, do I have to use nl2br() somehow? I tried it here: without the expressions it works: https://jsfiddle.net/uPw2U/170/

With expressions it doesn't: https://jsfiddle.net/gstcppgt/11/

Whats wrong with the {{ }} stuff?

10
  • 2
    This is link for your problem stackoverflow.com/questions/21097513/… Commented Jun 8, 2015 at 10:39
  • 1
    $scope.menu1 = $sce.trustAsHtml(data[0].text.Menu1); solve the problem with "grün" but the () still don't work: Test &#40;A,C,F&#41; Test1, Grün Commented Jun 8, 2015 at 10:45
  • 2
    jsfiddle.net/uPw2U/163 In this it () is working.just Commented Jun 8, 2015 at 11:02
  • 2
    you have to use it as <span ng-bind-html="html|html"> not {{html | html}} Commented Jun 9, 2015 at 5:38
  • 2
    docs.angularjs.org/api/ng/directive/ngBindHtml read it and vote up my comment if helped you Commented Jun 9, 2015 at 7:32

1 Answer 1

3

if the variable has html content you need to use $sce.trustAsHTML instead of expression.

for line breaks you can use style="white-space: pre;"

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I know, see the discussion above. It works like this: jsfiddle.net/uPw2U/182 instead of the css style I use a function nl2br. But isn't there a possibility to use the {{}} syntax with no-bind-html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.