Single / double quotes in ng-init
Let's say we are passing raw data from php to Angular 'by Mustache and the data is a string containing quotes like "Can not delete item". Mustache by default translates a single quote into '
as:
ng-init="message='Can't delete item'"
but this causes some Angular parsing issue:
Lexer error: Inexhaustible quote in columns ... ['] in expression [message = "Unable to delete item"]
I cannot use triple Mustache curls because then it would look like:
ng-init="message='Can't delete item'"
with the same error in the output.
Plunk: http://plnkr.co/edit/GCq4gLrD1NxxCvAsjHy9?p=preview
How can we elegantly address this in the Mustache stage?
source to share
I have created a directive to host your content, it will assign the body of the directive to the scope.message variable.
app.directive('myMessageVar',function(){
return{
restrict :'A',
scope:{
message:'=myMessageVar'
},
link: function (scope, iElement, iAttrs) {
scope.message=iElement.text();
iElement.text('');
}
}
})
In HTML
<span my-message-var="message">Can't delete item</span>
source to share
I ran into this problem today and I couldn't find an answer. But this piece of code works for me.
<?php $data_slashed = htmlentities(addslashes(html_entity_decode($data,ENT_QUOTES)),ENT_QUOTES); ?>
Where $data
is some data that was encoded withhtmlentities
.
Then you can do this
<p ng-bind-html= "data" ng-init="data='<?php echo $data_slashed; ?>'" />
I haven't tried mustache, but this works for my php template. Hope this helps someone else.
source to share
I believe you are using init incorrectly. From the ngInit docs:
The only suitable use of ngInit is for antialiasing the special ngRepeat properties, as shown in the demo below. Apart from this case, you should use controllers instead of ngInit to initialize values in scope.
For passing values from the server side, I think you should either use $ http (AJAX) or Module.constant
/ Module.value
from angular.Module .
For example:
// define a value
app.value('message','Can't delete item');
// define a constant
app.constant('constMessage', 'Can't delete item');
Then you can inject them into services or controllers. for example for a controller:
// use it in a controller
app.controller('someController', ['$scope', 'message', 'constMessage',
function($scope, message, constMessage) {
$scope.message = message;
$scope.constMessage = constMessage;
});
source to share