Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single/double quotes in ng-init

Let's say we are passing initial data from php to Angular' view by Mustache, and the data is some string that contain quotes, like "Can't delete item". Mustache by default translates the single quote to the ' like:

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

but that causes some kind of Angular parsing problem:

Lexer Error: Unterminated quote at columns ... ['] in expression [message='Can't delete item']

I can't use Mustache' triple curlies because then it will be like:

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

with the same error in output.

Plunk: http://plnkr.co/edit/GCq4gLrD1NxxCvAsjHy9?p=preview

How can we elegantly solve it on Mustache stage?

like image 368
Ivan Chernykh Avatar asked Dec 10 '25 05:12

Ivan Chernykh


1 Answers

I created a directive to place your content in, It will assign the body of directive to 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

like image 76
Sridhar Chidurala Avatar answered Dec 12 '25 22:12

Sridhar Chidurala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!