2016-03-22 6 views
0

Hallo Ich habe einen Filter, der \ n ersetzt n \ n auf \, wenn ich diesen Filter für meine Json Daten verwenden, erhalte ich Fehler sagen Can't interpolate: {{ post.description|nlToArray }} TypeError: Cannot read property 'replace' of undefined

Mein Filter

sampleApp.filter('nlToArray', function() { 
    return function(text) { 
     return text.replace(/\n/g, '/\n\n'); 
    }; 
}); 

Anzeigen von Daten von json api erhalten

HMTL

<p class="blog-post-title">{{ post.title }}</p> 
<p class="blog-post-meta"><i class="fa fa-clock-o">&nbsp {{ post.pub_date|date }}</i> </p> 
<span style="white-space: pre-line">{{ post.description|nlToArray }}</span> 
<br /> 

Meine json Daten

[{"id":1,"title":"Israel’s desert city of Beersheba is turning into a cybertech oasis","description":"morphing into a tech oasis.\r\nThe military’s massive relocation of its prestigious technology units.\r\nBeersheba has all of the ingredients of a vibrant security technology ecosystem, \r\n“All in all, projections are that 20,000-30,000 \r\nThe commercial sector has teamed up cyber attacks.","pub_date":"2016-03-20T10:48:19.394643Z"},{"id":2,"title":"These are testing times: mavericks vs. ice people","description":"One of my earliest engineering jobs, before I fled hardware in favor of the (relative). \r\nThe practice of engineering soon teaches one that, .\r\nSo what do we do? We practice defense in depth. We follow the robustness principle. We “always code as \r\n…Yeah, well, that’s the idea. For my day job at HappyFunCorp I do a lot of interviews, and almost every junior develope.\r\nI don’t necessarily blame them. You can make go.","pub_date":"2016-03-20T10:50:07.965930Z"}] 

viel klarer zu sein, ist die Beschreibung in json wie diese

morphing into a tech oasis.\r\nThe military’s massive relocation of its prestigious technology units.\r\nBeersheba has all of the ingredients of a vibrant security technology ecosystem, \r\n“All in all, projections are that 20,000-30,000 \r\nThe commercial sector has teamed up cyber attacks. 

Was die mögliche Lösung

+0

Was sehen Sie, wenn Sie den Filter entfernen? Dh, versuchen Sie einfach "post.description" anzuzeigen, so wie sie ist – Phil

+0

Ya, aber ich füge 2 neue Zeilenzeichen ein .......... dies wird durch Filter gemacht ...... Wenn ich Filter entferne, funktioniert es fein – Coeus

+0

Das ist seltsam. Sie sind sicher, dass Sie in den Funktionsargumenten keinen Tippfehler haben, dh es hat definitiv 'return function (text) {...' und nicht 'return function (txt)' oder irgendetwas anderes? – Phil

Antwort

1

den Code null Make ... Vielen Dank im Voraus sein könnte sicher durch Ersetzen

sampleApp.filter('nlToArray', function() { 
    return function(text) { 
     return text.replace(/\n/g, '/\n\n'); 
    }; 
}); 

mit

sampleApp.filter('nlToArray', function() { 
    return function(text) { 
     if (text) 
     return text.replace(/\n/g, '/\n\n'); 
     else 
     return text; 
    }; 
}); 
+0

Danke Harpreet ........ .... du hast gearbeitet ............ – Coeus

+0

@Coeus Ich bin mir überhaupt nicht sicher, warum das funktioniert hat. Bei einer Schätzung, würde ich sagen, mindestens eine Ihrer Dateneinträge hat ** nicht ** eine 'description' Eigenschaft – Phil

+0

ja ...... das ist wahr .... – Coeus