角度模板.replace [英] Angular Template .replace

查看:95
本文介绍了角度模板.replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在指令内部的模板中执行.replace

I am trying to do .replace within the template inside of the directive

directive('sampleComponent', function () {
return {
template: '<h2 style="border:1px solid red">{{data.Title}}</h2>'
};
})

我想在{{data.Title}}

i wan to do replace(/'/g, '"') on the {{data.Title}}

有什么建议吗?

推荐答案

我认为最好的方法是使用过滤器.

I think the best way is using a filter.

您可以创建自己的过滤器,例如:

You can create you filter like:

angular.module('myApp', [])
.filter('myReplace', function() {
  return function(input) {
    var out = ... //your replace logic
    return out;
  };
});

然后将其应用于您的模板:

And then apply it to your template:

directive('sampleComponent', function () {
  return {
    template: '<h2 style="border:1px solid red">{{data.Title | myReplace}}</h2>'
  };
})

请记住将过滤器注入到指令控制器中.

Remember to inject the filter in your directive controller.

这篇关于角度模板.replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆