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?

+3


source to share


8 answers


Just avoid the quote:



ng-init="message='Can\'t delete item'"

      

+1


source


Erase the single quote with the backslash \

following:



ng-init="message='Can\'t delete item'"

      

+1


source


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&#039;t delete item</span>

      

Plunkr: http://plnkr.co/edit/QRtXLX1IiGS6VV0Rc78I?p=preview

+1


source


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.

+1


source


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&#039;t delete item');

// define a constant
app.constant('constMessage', 'Can&#039;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;
});

      

0


source


Try switching between double quotes and single quotes.

so instead of

ng-init="message='{{delete_message}}'"

      

try it

ng-init='message="{{delete_message}}"'

      

0


source


This might work if your problem is limited to single quotes

ng-init='message="Can&#039;t delete item"'

      

0


source


Try entering the code, which should work.

<body ng-controller="MainCtrl" ng-init='message="Can&#039;t delete item"'> <p>Message: {{message}}</p> </body>

0


source







All Articles