2015-02-12 46 views
0

我有一个包含一些奇怪的字符,引用一个类似的长字符串:消毒变量在PHP中AngularJS

xxxxxxxxxxxxxxxxxxxxxxxxxxxxdewhriho“shfihewr32” fhihifhewt43t [sdfhiort] sfhishf“GG” fhdif

它的一部分对象我在我的脚本开始时创建的PHP文件中,它以字符串形式存储在$ song-> lyrics属性中。我试着将它传递给我的AngularJS控制器通过此:

<div ng-controller="lyricsMod" ng-init="songInit('<?php echo $song->lyrics')"> 

这是我的AngularJS控制器:

displaySong.controller('lyricsMod', ['$scope', function($scope) { 
//Set up initial scopes: 
$scope.songInit = function(songLyrics) { 
    $scope.lyrics = songLyrics; 
} 

但我不断收到来自AngularJS这个错误:

Error: [$parse:lexerr] 
https://docs.angularjs.org/error/$parse/lexerr?p0=Unterminated 

我已尝试在字符串上使用mysqli_real_escape_string无济于事。当AngularJS碰到字符串中的第一个引号时,错误似乎就会发生。我读过你需要对字符串进行清理,但我不知道如何清理PHP或HTML中的字符串。对不起,如果这是一个noob问题,Ijust开始学习AngularJS。有没有更好的做法将我的PHP变量传递到AngularJS中,或者我在这里丢失了什么?

+0

你有没有尝试看看[ngSanitize](https://docs.angularjs.org/api/ngSanitize) – uniquerockrz 2015-02-12 03:56:55

回答

1

你没有关闭你的PHP代码块(你错过了?>)。

而且,我相信它需要JSON编码的插入JS(这也将不需要用引号将PHP块),以及HTML实体化以插入HTML ATTR:

<div ng-controller="lyricsMod" ng-init="songInit(<?php echo htmlentities(json_encode($song->lyrics)); ?>)">