angularjs——插值字符串
一、何为插值字符串?
其实插值字符串的意思就是:拥有字符插值标记的字符串。
如: 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——插值字符串的更多相关文章
- AngularJS 插值字符串 $interpolate
定义: $interpolate:编译一段带有嵌入标记的语句,然后返回一个interpolate(插值)函数.使用: $interpolate(text,[mustHaveException],[tr ...
- Angular表达式--插值字符串($interpolate)
要在字符串模板中做插值操作,需要在你的对象中注入$interpolate服务.在下面的例子中,我们将会将它注入到一个控制器中: angular.module('myApp', []) .control ...
- angularJs,ionic字符串操作
1.首先我们需要把一段"文本或字符串"中的我们想进行操作的"字符串","字"筛选出来,代码如下: app.filter('replaceCo ...
- angularJs中将字符串转换为HTML格式
首先定义一个filter: .filter( 'to_trusted', ['$sce', function ($sce) { return function (text) { return $sce ...
- AngularJS 截取字符串
参考文章:https://blog.csdn.net/u010234516/article/details/54631525 //过滤器 app.filter('textLengthSet', fun ...
- Angular 插值字符串
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Groovy--使用模板引擎和GroovyShell执行插值字符串
package curveJudge import groovy.text.SimpleTemplateEngine /** * Created by Jxy on 2019/8/26 17:16 * ...
- Swift字符串插值
字符串插值是一种全新的构建字符串的方式,可以在其中包含常量.变量.字面量和表达式.您插入的字符串字面量的每一项都被包裹在以反斜线为前缀的圆括号中: let multiplier = let messa ...
- angularjs杂谈
1.MVVM的看法:我给view里面各种控件也定义一个对应的数据对象,这样,只要修改这个数据对象,view里面显示的内容就自动跟着刷新,而在view里做了任何操作,这个数据对象也跟着自动更新. Vie ...
随机推荐
- 【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制
本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...
- MySQL5.6忘记root密码(win平台)
1.首先net stop mysql服务,并且切换到任务管理器,有与mysql有关的,最好关闭进程. 2.运行CMD命令切换到MySql安装bin目录,下面是我的mysql安装目录 cd C:\Pr ...
- final .....finally ...... 和Finalize ......区别
一.性质不同 ()final为关键字: ()finalize()为方法: ()finally为为区块标志,用于try语句中: 二.作用 ()final为用于标识常量的关键字,final标识的关键字存储 ...
- 使用KeleyiSQLHelper类进行分页查询
本文适用于sql server单主键表或者视图进行分页查询,支持多字段排序. KeleyiSQLHelper类的最新代码请到http://hovertree.com/down/下载整个解决方案源代码查 ...
- ASP.NET Core 发布至Linux生产环境 Ubuntu 系统
ASP.NET Core 发布至Linux生产环境 Ubuntu 系统,之前跟大家讲解了 dotnet publish 发布,而没有将整个系统串起来. 今天就跟大家综合的讲一下ASP.NET Core ...
- Expression<Func<TObject, bool>>与Func<TObject, bool>的区别
Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...
- iOS阶段学习第14天笔记(NSString与NSMutableString)
iOS学习(OC语言)知识点整理 一.OC字符串的操作 1)OC中字符串分为两种: 1.不可变字符串NSString:不能修改对象内容,但是可以改变对象的指针. 2.可变字符串NSMutableStr ...
- 使用Aspose插件对Excel操作
使用使用Aspose插件对Excel文档进行导入导出操作 使用前请先下载Aspose插件引用 Excel导入: 前台使用file标签获取,submit方式提交. <form id="f ...
- 百度云推送-服务端 C# SDK
思路: 1.公司有项目需要做android和ios手机端推送消息的功能: 2.没有接触过这方面的知识,一头雾水,开始在网上一顿搜,网上倒是有不少解决方案,首先搜的是android的解决方案,因为ios ...
- 不可或缺 Windows Native 系列文章索引
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...