`
张江涛
  • 浏览: 5324 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

angular

阅读更多
$interpolate

编译一段带有插入标记的语句,然后返回一个interpolation(插值)函数,
这个服务也被HTML$compile服务用来进行数据绑定。可以用$interpolateProvider来配置插入的标记。
依赖

$parse
$sce
使用

$interpolate(text, [mustHaveExpression], [trustedContext], [allOrNothing])
参数

text[string] 需要被编译的字符串
mustHaveExpression[boolean] 如果这个参数的值被设置为true,那么上面的text中必须含有嵌入其中的表达式,不然将会返回null,而不是我们预期的interpolation function
trustedContext[string] 如果提供了这个参数值,那么在返回相应的函数之前,将会使用$sce.getTrusted(interpolatedResult, trustedContext)对返回的结果做处理。
allOrNothing[boolean] 如果这个参数的值被设置为true,那么只有text中所有嵌入的表达式没有一个被转换为undefined的时候才会返回我们期望的函数。
返回值

function(context) 一个用来计算带有插值标记语句的函数,这个函数有一个参数

context 为插入标记语句中的表达式提供的上下文。
Escaped Interpolation(摆脱插值服务)

$interpolate提供了一种用来摆脱插值标记的机制,那就是通过在插入标记的开始和结束符号前面加上反斜杠\,AngularJS将会把这部分渲染为普通的部分,所以也不会被解读为为表达式或者数据绑定。
这让Web服务器阻止脚本注入和抵御网络攻击成为可能,从某种程度上说,无需依靠ngNonBindable指令就可以展示代码例子是如何运行的。
为了安全目的,我们强烈建议Web服务器对用户的应用数据进行检索和过滤,用&lt和&gt替代(<,>),并且使用相应的字符去替代插入标记的开始和结束符。
如果你觉得笔者翻译的不是很好,可以到官网看看他们的英文文档

下面我们来探究一下$interpolate的使用方法

注:代码是在jsfiddle上面写的,所以大家实践的时候要注意引入相应的文件
T1:在这个例子中我们先来简单地使用一下$interpolate
(html)

html<div ng-app="MyApp">
    <div ng-controller="MyController">
        <input ng-model="myName" type="text" placeholder="Type Your Name">
        <textarea ng-model="myTextarea" cols="30" rows="10"></textarea>
        <div ng-bind="interpolatedValue"></div>
    </div>
</div>
(js)

jsangular.module("MyApp", [])
.controller("MyController", function($scope, $interpolate){
    $scope.$watch("myTextarea", function(newVal, oldVal, scope){
        var interpolatedFunc = $interpolate(newVal);
        //(1)
        $scope.interpolatedValue = interpolatedFunc({myName: $scope.myName});
        //(2)
        //$scope.interpolatedValue = interpolatedFunc(scope);       
    });

});
关于上面代码的解释:

因为要实时监测myTextarea的变化,所以我们要使用$watch方法去对其进行实时监测。
代码(1)与代码(2)的作用是一样的,括号中的参数是插值标记字符串中表达式转义(解析)的上下文。
我们在textarea中输入{{myName}}会在下面的div中显示input输入框的内容。
T1:jsfiddle代码在这里

T2:在下面这个例子中我们尝试去使用$interpolateProvider
只需修改上面的js代码如下所示:

jsangular.module("MyApp", [])
    .config(['$interpolateProvider', function ($interpolateProvider) {
    $interpolateProvider.startSymbol('$');
    $interpolateProvider.endSymbol('$');
}])
.controller("MyController", function($scope, $interpolate){
    $scope.$watch("myTextarea", function(newVal, oldVal, scope){
        var interpolatedFunc = $interpolate(newVal);
        $scope.interpolatedValue = interpolatedFunc({myName: $scope.myName});
        //$scope.interpolatedValue = interpolatedFunc(scope);       
    });

});
上面的代码修改了插入标记,让插入标记的开始和结束符都变成了$,然后我们也就可以在textarea中输入$myName$会在下面的div中显示input输入框的内容。
分享到:
评论

相关推荐

    angular6 中文官方文档

    Angular 本身使用 TypeScript 写成 的。它将核心功能和可选功能作为一组 TypeScript 库进行实现,你可以把它们导入你的应用中。 Angular 的基本构造块是 NgModule,它为组件提供了编译的上下文环境。 NgModule 会把...

    Switching to Angular 2

    Key Features, Get up to date with the latest changes to Angular 2, including the improvements to directives, change detection, dependency injection, router, and moreUnderstand Angular 2's new ...

    angular4强制刷新视图的方法

    使用angular的过程中有时会出现数据已经更新了,但是对于的视图没有更新,针对这一情况,可以是用angular提供的方法强制更新视图。 这里使用NGZone来更新视图 import {NgZone} from '@angular/core'; constructor...

    Angular-angular2-mdl.zip

    Angular-angular2-mdl.zip,基于Material Design Lite的Angular 2、4、5、6、7、8组件、指令和样式(NPM:@Angular MDL/Core)基于Material Design Lite的Angular 8组件、指令和样式https://getmdl.io(v:1.3.0)。...

    Angular 2 Cookbook, 2nd Edition, 2017

    Angular 2 introduces an entirely new paradigm of applications. It wholly embraces all the newest concepts that are built into the next generation of browsers, and it cuts away all the fat and bloat ...

    angular-2-cookbook-2017(Matt Frisbie)高清PDF完整书签

    Chapter 1, Strategies for Upgrading to Angular 2, is an overview of a number of ways to migrate an Angular 1 application to Angular 2. Although there is no one-size-fits-all upgrade strategy, you will...

    Angular-angular-electron-dream-starter.zip

    Angular-angular-electron-dream-starter.zip,Angular Electron初学者工具包,包括Webpack、Angular 4(路由器、HTTP、表单、服务、NGRX、测试、E2E、覆盖范围)、Karma、Spectron、Jasmine、伊斯坦布尔,以及带有...

    Learn Angular The Collection

    Learn Angular: The Collection By 作者: David Aden Pub Date: 2018 ISBN: n/a Pages: 657 Language: English This book is for all front-end developers who want to become proficient with Angular and its ...

    Angular2环境搭建

    angular2工程搭建 一、编译和运行环境搭建 1、node.js和npm安装 a、下载地址:https://nodejs.org/en/download/ b、通过node -v 和 npm -v检查是否正确安装 c、下载工程: 略 d、进入工程目录 cd ...

    angular2.pdf

    angular2文档,We developed this book to be used as course material for Rangle's Angular 2 training, but many people have found it to be useful for learning Angular 2 on their own. This book will cover...

    angular-1.3.14.zip

    从官网angularjs org扒下来的 包含angular js angular min js angular min js map angular route js angular route min js之类等等 AngularJS是为了克服HTML在构建应用上的不足而设计的 它通过新的属性和表达式...

    angular-1.3.5.zip

    从官网angularjs org扒下来的 包含angular js angular min js angular min js map angular route js angular route min js之类等等 AngularJS是为了克服HTML在构建应用上的不足而设计的 它通过新的属性和表达式...

    Angular 2 By Example

    Angular 2 By Example by Chandermani Arora English | 4 Nov. 2016 | ISBN: 178588719X | 458 Pages | MOBI/EPUB/PDF | 12.97 MB Angular 2 will help you build faster, more efficient, and more flexible cross...

    Angular-flask-angular-data-science.zip

    Angular-flask-angular-data-science.zip,数据科学入门应用程序的存储库,使用Flask、Angular和Docker。...

    ng-book2-book-angular-6-r70

    ng-book2-book-angular-6该书最新版R70. This book aims to be the single most useful resource on learning Angular. By the time you’re done reading this book, you (and your team) will have everything you...

    Switching to Angular 3rd 第三版

    Align your work to stable APIs of Angular, version 5 and beyond, with Angular expert Minko Gechev. Angular is the modern Google framework for you to build high-performance, SEO-friendly, and robust ...

    angular-1.0.8.zip

    从官网angularjs org扒下来的 包含angular js angular min js angular min js map angular route js angular route min js之类等等 AngularJS是为了克服HTML在构建应用上的不足而设计的 它通过新的属性和表达式...

    Angular: Up and Running: Learning Angular, Step by Step2018

    Chapter 1, Introducing Angular, is an introduction to Angular as well as the concepts behind it. It also covers what it takes to start writing an Angular application. Chapter 2, Hello Angular, walks...

    Learning Angular 2 [2016]

    Learning Angular 2 English | 31 May 2016 | ISBN: 1785882074 | 352 Pages | AZW3/MOBI/EPUB/PDF (conv) | 6.1 MB Your quick, no-nonsense guide to building real-world apps with Angular 2 About This Book ...

Global site tag (gtag.js) - Google Analytics