一、何为插值字符串?

其实插值字符串的意思就是:拥有字符插值标记的字符串。
如: hello,{{ to }}....
字符插值标记:相当于我们平时在字符串替换中使用到的占位符。
上面的例子中的{{to}}其实就是类似于一个占位符,我们可以通过$interpolate服务将上面的例子中的字符串进行处理,返回一个模板对象,由于$interpolate服务默认是将{{}}分别当做是占位符的前缀和后缀,所以,上面的例子中的{{to}}将会被当做一个占位符,并且把to当做该占位符的命名,可以给模板对象传入一个对象如:{to:'xxxx'},直接替换掉{{to}}占位符。

二、来个栗子!

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-app="myApp">
<div ng-controller="MyController">
<input ng-model="to"
type="email"
placeholder="Recipient" />
<textarea ng-model="emailBody"></textarea>
<pre>{{ previewText }}</pre>
<script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script>
</div>
</body>
</html>
angular.module('myApp',[])
.controller('MyController',function($scope,$interpolate){
$scope.emailBody = "hello,{{to}}";
$scope.to = 'ari@fullstack.io';
var template = $interpolate($scope.emailBody);
$scope.$watch('to',function(to){
console.log(to);
$scope.previewText = template({'to':$scope.to});
});
});

代码片段:JS Bin on jsbin.com


三、$interpolate服务

$interpolate服务是一个可以接受三个参数的函数,其中第一个参数是必需的。

  • text(字符串):一个包含字符插值标记的字符串。
  • mustHaveExpression(布尔型):如果将这个参数设为true,当传入的字符串中不含有表
    达式时会返回null。
  • trustedContext(字符串): AngularJS会对已经进行过字符插值操作的字符串通过
    $sec.getTrusted()方法进行严格的上下文转义。

$interpolate服务返回一个函数,用来在特定的上下文中运算表达式。

四、如何改变占位符的前、后缀?

上面的第一点就讲到了$interpolate默认的占位符是以{{}}为前后缀的。
那这个占位符的前后缀能否改成我们自己想要的样子呢?
答案是肯定的,当然可以改~~
我们只需要在$interpolateProvider服务进行配置。

$interpolateProvider.startSymbol('__');
$interpolateProvider.endSymbol('__');

注意:使用$interpolateProvider进行设置这个占位符的前后缀的时候,需要注意你的上下文,如果你是直接在某个模块下进行配置,那么该模块的angularjs数据绑定对象的{{xxx}}格式也会被影响。
如:

angular.module('myApp',[])
.config(['$interpolateProvider',function($interpolateProvider){
$interpolateProvider.startSymbol('__');
$interpolateProvider.endSymbol('__');
}])
.controller('MyController',function($scope,$interpolate){
$scope.emailBody = "hello,__to__";
$scope.to = 'ari@fullstack.io';
$scope.previewText ="";
var template = $interpolate($scope.emailBody);
$scope.$watch('to',function(to){
console.log(to);
$scope.previewText = template({'to':$scope.to});
console.log($scope.previewText);
});
});

那么html页面中{{previewText}}就也要改成__previewText__的格式.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-app="myApp">
<div ng-controller="MyController">
<input ng-model="to"
type="email"
placeholder="Recipient" />
<br/>
<textarea ng-model="emailBody"></textarea>
<br/>
<pre>__previewText__</pre>
<script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script>
</div>
</body>
</html>

所以,一般情况下$interpolateProvide用在自己创建的服务上面,这样就可以把上下文环境进行隔离。

代码片段:JS Bin on jsbin.com

angularjs——插值字符串的更多相关文章

  1. AngularJS 插值字符串 $interpolate

    定义: $interpolate:编译一段带有嵌入标记的语句,然后返回一个interpolate(插值)函数.使用: $interpolate(text,[mustHaveException],[tr ...

  2. Angular表达式--插值字符串($interpolate)

    要在字符串模板中做插值操作,需要在你的对象中注入$interpolate服务.在下面的例子中,我们将会将它注入到一个控制器中: angular.module('myApp', []) .control ...

  3. angularJs,ionic字符串操作

    1.首先我们需要把一段"文本或字符串"中的我们想进行操作的"字符串","字"筛选出来,代码如下: app.filter('replaceCo ...

  4. angularJs中将字符串转换为HTML格式

    首先定义一个filter: .filter( 'to_trusted', ['$sce', function ($sce) { return function (text) { return $sce ...

  5. AngularJS 截取字符串

    参考文章:https://blog.csdn.net/u010234516/article/details/54631525 //过滤器 app.filter('textLengthSet', fun ...

  6. Angular 插值字符串

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Groovy--使用模板引擎和GroovyShell执行插值字符串

    package curveJudge import groovy.text.SimpleTemplateEngine /** * Created by Jxy on 2019/8/26 17:16 * ...

  8. Swift字符串插值

    字符串插值是一种全新的构建字符串的方式,可以在其中包含常量.变量.字面量和表达式.您插入的字符串字面量的每一项都被包裹在以反斜线为前缀的圆括号中: let multiplier = let messa ...

  9. angularjs杂谈

    1.MVVM的看法:我给view里面各种控件也定义一个对应的数据对象,这样,只要修改这个数据对象,view里面显示的内容就自动跟着刷新,而在view里做了任何操作,这个数据对象也跟着自动更新. Vie ...

随机推荐

  1. Android入门(七)碎片的生命周期与限定符

    原文链接:http://www.orlion.ga/560/ 这篇文章实际已经在上篇文章中写的差不多了,但是万恶的wordpress没保存!已经不止一次出现这种情况了! 一.碎片的生命周期 1.碎片的 ...

  2. Cwinux源码解析(三)

    我在我的 薛途的博客 上发表了新的文章,欢迎各位批评指正. Cwinux源码解析(三)

  3. Java多线程系列--“JUC原子类”03之 AtomicLongArray原子类

    概要 AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray这3个数组类型的原子类的原理和用法相似.本章以AtomicLongArray对数 ...

  4. java之Maven配置和springMvc的简单应用

    初始springMvc这个框架,非常的陌生,而且幸好公司是通过maven这个代码管理工具,可以随时添加依赖.解决了很多问题在以后深入开发中. 项目结构: 通过结构中,pom.xml这个文件其实就说明这 ...

  5. 别用symbolicatecrash来解析crash Log了

    今天突然发现了一个解析iOS crash log的好方法,忍不住来分享一下. 相信每个做iOS开发的TX都应该不会对symbolicatecrash陌生,我们第一次遇到真机上产生的崩溃日志时,在网上搜 ...

  6. 吐槽坑爹的微软win store app审核

    从学习win store app 开发到做出第一个应用 博客园cnblogs 花了一个多月的全部业余和上班空闲时间, 上周在端午节放假期间终于完成了计划的全部开发和测试, 6月10号怀着无比激动的心情 ...

  7. Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接

    <Windows Azure Platform 系列文章目录> 我们知道,Azure Virtual Network可以 1.将对台Azure VM加入到同一个网段里,同时绑定内网IP地址 ...

  8. 研究 研究而已 java和.net的HashSet对比

    各位看官,,我不是在引发战争,,我只是想知道事情的真想,我源之于,一段代码我需要实现C#和java的两个版本功能,才发现这一个对比.讨论问题的实质,为什么会出现这样的问题.java和C#都很优秀.请大 ...

  9. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  10. 【转载】css3 content 生成内容

    content一般和:before,:after一起使用,用来生成内容(img和input没有该属性),content的内容一般可以为以下四种: none: 不生成任何值. attr: 插入标签属性值 ...