创建angular指令以在创建html时重用代码解析错误
Creating angular directive to reuse code - parse error while creating html
下面是我正在创建的控件的代码片段。这种控制在不同的地方使用,变量也不同。
我正试图编写一个指令来清理代码,但在{{}}附近插入值时出现解析错误。
新到棱角分明,无法确定我缺少的是什么。请帮忙。
轨迹编辑是另一个指令。
原始控制代码:
<div id="text-wrapper">
<div track-edit="true" id="Type1Desc_{{t1Card.id}}" class="textbody" ng-blur="SaveDesc('Type1Desc_'+t1Card.id,t1Card.Description,'Type1')">
<div>
<p><div spellcheck="true" ng-bind-html="t1Card.OrigDescription|diff:t1Card.Description"></div></p>
</div>
</div>
</div>
指令代码
app.directive('customEditor', function () {
return {
restrict: "E",
scope: {
fId: "@",
idAppend: "@",
className: "@",
origVal: "@",
currVal: "@"
},
replace: true,
transclude: false
template: ' <div id="text-wrapper"><div track-edit="true" id="{{idAppend}}_{{fId}}" ' +
'class="{{className}}" ><div><p>' +
'<div spellcheck="true" ng-bind-html="{{origVal}}|diff:{{currVal}}"></div></p></div></div></div>',
link: function (scope, element, attrs) {
}
}
});
Html指令后:
<custom-editor fid="{{t1Card.id}}" idappend="Type1Desc" classname="textbody" ng-blur="SaveLineItemDesc('Type1Desc_'+t1Card.id,t1Card.Description,'Type1')" origVal="{{t1Card.OrigDescription}}" currVal="{{t1Card.Description}}">
</custom-editor>
我认为你的错误在于这一部分:
fId: "@",
idAppend: "@",
className: "@",
origVal: "@",
currVal: "@"
应该是:
fid: "@",
idappend: "@",
classname: "@",
origVal: "@",
currVal: "@"
在指令中:
<custom-editor fid="{{t1Card.id}}" idappend="Type1Desc" classname="textbody" ng-blur="SaveLineItemDesc('Type1Desc_'+t1Card.id,t1Card.Description,'Type1')" origVal="{{t1Card.OrigDescription}}" currVal="{{t1Card.Description}}">
</custom-editor>
您有idappend,但您指的是idappend。这是错误的。您应该将其称为idappend。这同样适用于fid和类名,它们应该指没有camelcase格式的情况
编辑代码-
如果您信任origVal和currValvalue,则替换以下语句:
ng-bind-html="{{origVal}}|diff:{{currVal}}"
有了这个
ng-bind-html-unsafe="{{origVal}}|diff:{{currVal}}"
或者你可以像这个一样使用$sce
$sce.parseAsHtml(your_data_value)
要了解更多信息,您也可以参考这篇文章。删除了ng-bind-html unsafe后,如何注入html?
相关文章:
- 创建一个循环来简化HTML和CSS代码
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- Markdown模式代码镜像正在创建
- 如何在Google chrome安全首选项文件中创建扩展安全哈希代码
- 创建对象函数原型和代码是错误的
- 如何在不编写样板代码的情况下在Node中创建可重用函数
- 代码挑战:创建一个跟踪对象实例总数的类Foo
- 正在从帧创建时间代码
- 如何使用我的Jquery代码创建委托事件侦听器
- JavaScript代码优化-创建可重用类
- 如何在jquery中为object键创建一个工作变量以避免额外的代码
- 使用Javascript创建的Cookie在C#代码中不可访问..任何原因或其他选择
- 使用Promise来创建“;原子“;Javascript中的代码块
- HTML 运行时创建代码非常慢
- 建议为选择图像创建代码
- 运行Greasemonkey在DOM中创建代码
- 创建代码以逐个运行函数(DOM)的一些想法
- 从Web表单创建代码片段(易于剪切和粘贴)
- 你能为iPhone创建代码吗;添加到主屏幕'按钮放在网页上
- 如何在组合中创建代码