简体   繁体   中英

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?

逃避报价:

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

使用backslash \\逃避单引号backslash \\如下:

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

Try switching the double quotes and the single quotes.

so instead of

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

try

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

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

I came across with this problem today and I did not find much of 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 were encoded using htmlentities .

Then you can now do this

<p ng-bind-html= "data" ng-init="data='<?php echo $data_slashed; ?>'" />

I have not tried mustache but this works for my php template. Hope this help someone else.

I believe you're using init incorrectly. From ngInit docs:

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

For passing values from your 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');

You can then inject them into services or controllers. eg, for a controller:

// use it in a controller
app.controller('someController', ['$scope', 'message', 'constMessage', 
function($scope, message, constMessage) {
    $scope.message = message;
    $scope.constMessage = constMessage;
});

如果您的问题仅限于单引号,则可能会有效

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

尝试下面的代码它应该工作。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM