angularjs - 如何在 AngularJS 中使用 Lodash _.template
问题描述
你能解释一下如何使用 Lodash_.template
来通过 .html 文件从脚本中获取一些价值{{data}}
吗?
例如:
在script.js中,我对我的数据对象和view.html进行了一些更改。我想data
改变{{data}}
如何使用 lodash_.template()
解决这个问题?
解决方案
lodash 模板功能不允许您从文件构建模板(但您可以与 Webpack 结合使用)。它与 AngularJS 模板完全无关。然而,在某些情况下,在 AngularJS 内部使用是有意义的,例如构建带有模式等的字符串列表。
您将模板字符串编译为一个函数,您可以根据需要随时调用这些值:
const executor = _.template("Hello <%= name %>, my old friend!");
console.log(executor({name: 'Alice'})); // Hello Alice, my old friend!
console.log(executor({name: 'Bob'})); // Hello Bob, my old friend!
您还可以使用条件、循环等:https ://lodash.com/docs/4.17.15#template
推荐阅读
- reactjs - react-slick, slickNext 在上一次渲染调用时在重新渲染后自动触发
- sql - 找到最高总价并转换
- database - 表中的单个列有多个索引是否很好?
- automation - 量角器:错误:ReferenceError:未定义可描述
- ios - 在另一个视图控制器中使用来自一个视图控制器的变量
- android - Android TV 广告无法在 Amazon Fire TV 上正常播放
- git - 如何将 monorepo 拆分为多个团队,每个团队只能访问一部分 repo?
- python - 将变量增加到最大大小
- d - 无法使用 Range 初始化 D 结构成员
- java - 这里发生了什么:目标 org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test 的执行默认测试失败