博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angularjs绑定属性_AngularJS隔离范围绑定表达式教程
阅读量:2532 次
发布时间:2019-05-11

本文共 3023 字,大约阅读时间需要 10 分钟。

angularjs绑定属性

We have seen how to use local scope properties to pass values into a and how to bind to external objects using a In this post, we are going to look at the “&” which is used to call an expression on the parent scope from the isolated scope.

我们已经看到了如何使用局部作用域属性将值传递给以及如何使用绑定到外部对象 在本文中,我们将研究“&” ,它用于从隔离范围调用父范围的表达式。

AngularJS隔离范围“&”示例 (AngularJS Isolate Scope “&” Example)

The “&” local scope property allows the consumer of a directive to pass in a function and directive can invoke whenever it wants to.

In this section, we are going to explain isolate scope “&” with an example. We will create a myEmployee directive to display an input text field and a button. When the user clicks on the button, it will notify the controller and alert a message using the input value.

“&”局部作用域属性允许指令的使用者传入函数,并且指令可以在需要时调用。

在本节中,我们将通过示例解释隔离范围“&”。 我们将创建一个myEmployee指令以显示输入文本字段和一个按钮。 当用户单击按钮时,它将通知控制器并使用输入值警告消息。

The following example demonstrates this usage.

以下示例演示了此用法。

app.js

app.js

var app = angular.module('mainApp', []);	app.controller("MainCtrl", function($scope){		$scope.clickMe = function(message){			alert(message);		}	});	app.directive("myEmployee", function() {  	return {      		scope:{			send:"&"		},		template: 'Type Something: '+			  ''  	};	});
  • Create a controller MainCtrl  and define a function clickMe() in its scope.

    创建一个控制器MainCtrl并在其范围内定义一个函数clickMe()
  • Then we create a myEmployee directive.

    然后,我们创建一个myEmployee指令。
  • The directive creates a custom local scope property within its isolate scope named send. This is done using scope { send: "&" }. In this example,send is just an alias for clickMe function.  When send is invoked, the clickMe function that was passed in will be called.

    该指令在其名为send的隔离范围内创建一个自定义本地范围属性。 这是通过使用scope { send: "&" } 。 在此示例中, send只是clickMe函数的别名 调用send时, 调用传入的clickMe函数。
  • The template within the directive contains a text field and  a button.

    指令中的模板包含一个文本字段和一个按钮。
  • The button displays only when something is typed in the input field ( ng-show="inputMsg").

    仅当在输入字段( ng-show="inputMsg" )中键入内容时,该按钮才会ng-show="inputMsg"
  • When the button is clicked the scope property send, which is really a reference to the clickMe function that was passed in) can then be invoked.

    单击按钮时可以调用scope属性send (实际上是对传入的clickMe函数的引用)。
  • We have used this special syntax to pass the input value send( {message : inputMsg}

    我们使用了这种特殊语法来传递输入值send( {message : inputMsg}
  • The following code shows how to use our directive and pass the clickMe function which is defined in the controller to the scope property, send

    以下代码显示了如何使用我们的指令并将控制器中定义的clickMe函数传递给scope属性,并发送
  •  The clickMe function in the controller will alert our passed in message.

    控制器中的clickMe函数将提醒我们传入的消息。

index.html

index.html

          
AngularJS Isolate Scope

You will see the following output on your browser.

您将在浏览器中看到以下输出。

That’s all for now and we will see more AngularJS features in the upcoming posts.

目前仅此而已,我们将在后续文章中看到更多AngularJS功能。

翻译自:

angularjs绑定属性

转载地址:http://uplzd.baihongyu.com/

你可能感兴趣的文章
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>