简体   繁体   English

ng-init中的单引号/双引号

[英]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将初始数据从php传递到Angular的视图,并且数据是一些包含引号的字符串,例如“无法删除项目” Mustache by default translates the single quote to the ' Mustache默认将单引号转换为' like: 喜欢:

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

but that causes some kind of Angular parsing problem: 但这会导致某种Angular解析问题:

Lexer Error: Unterminated quote at columns ... ['] in expression [message='Can't delete item'] Lexer错误:列中的未终止引用...表达式中的['] [message ='无法删除项目']

I can't use Mustache' triple curlies because then it will be like: 我不能使用Moustache的三重卷曲,因为它会像:

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

with the same error in output. 输出中出现相同的错误。

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

How can we elegantly solve it on Mustache stage? 我们如何在Mustache舞台上优雅地解决它?

逃避报价:

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. 我创建了一个指令来放置你的内容,它会将指令体分配给scope.message变量。

app.directive('myMessageVar',function(){
  return{
            restrict :'A',
            scope:{
              message:'=myMessageVar'
            },
            link: function (scope, iElement, iAttrs) {
               scope.message=iElement.text(); 
               iElement.text('');
          }
   }

})

In HTML 在HTML中

 <span my-message-var="message">Can&#039;t delete item</span>

Plunkr: http://plnkr.co/edit/QRtXLX1IiGS6VV0Rc78I?p=preview 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 . 其中$data是使用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. 我没有尝试过胡子,但这适用于我的php模板。 Hope this help someone else. 希望这有助于其他人。

I believe you're using init incorrectly. 我相信你错误地使用了init。 From ngInit docs: 来自ngInit文档:

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. ngInpe的唯一合适用途是用于别名ngRepeat的特殊属性,如下面的演示所示。 Besides this case, you should use controllers rather than ngInit to initialize values on a scope. 除了这种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。

For passing values from your server-side I think you should either use $http (AJAX) or Module.constant / Module.value from angular.Module . 对于从服务器端传递价值,我认为你应该要么使用$ HTTP (AJAX)或Module.constant / Module.valueangular.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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