angular指令中@,=,&的区别

本文介绍了AngularJS中directive的三种绑定方式:@(单向数据绑定)、=(双向数据绑定)和&(函数绑定)。通过实例展示了如何在指令中使用这些绑定策略来实现与外部作用域之间的数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当directive中的scope设置为一个对象的时候,该指令就有了一个独立的作用域,AngularJS提供了一种绑定策略用于隔离作用域和外部作用域进行通信。

1、@(or @attr)

使用@符号可以进行单项的数据绑定,取值总是一个字符串,所以要用{{}}。

另外这也是一个单向的绑定,外部数据改变会反应到内部,但是内部数据变数据变化,外部不会变。

属性要用-连接,scope中写它的驼峰格式。

如果没有通过@attr指定属性名称,那么本地名称要与DOM属性的名称一致。

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS</title>
</head>
<body>
    <div ng-controller="parent">
        <div>
            <input type="text" ng-model="name"/>
        </div>
        <my-name show-name="{{name}}">
        
        </my-name>
    </div>
</body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller("parent", function($scope){
        $scope.name = "Jhon";
    }).directive("myName", function(){
        return {
            restrict:"EA",
            scope:{
                showName: '@'
                // name: '@showName'
            },
            template:'<input type="text" ng-model="showName"/>',
            // template:'<input type="text" ng-model="name"/>',
        }
    });
</script>
</html>

 

 

2、= (or =attr)
使用=进行双向数据绑定,任何一方的值改变都会反应到另一方。因为是双向绑定,所以不要使用{{}},不然以下demo会报错。

属性要用-连接,scope中写它的驼峰格式。

如果没有通过@attr指定属性名称,那么本地名称要与DOM属性的名称一致。

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS</title>
</head>
<body>
    <div ng-controller="parent">
        <div>
            <input type="text" ng-model="name"/>
        </div>
        <my-name show-name="name">
        
        </my-name>
    </div>
</body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller("parent", function($scope){
        $scope.name = "Jhon";
    }).directive("myName", function(){
        return {
            restrict:"EA",
            scope:{
                showName: '='
            },
            template:'<input type="text" ng-model="showName"/>'
        }
    });
</script>
</html>

 

 

3、&(or &attr)
&用来绑定外部的函数。

属性要用-连接,scope中写它的驼峰格式。

如果没有通过@attr指定属性名称,那么本地名称要与DOM属性的名称一致。

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS</title>
</head>
<body>
    <div ng-controller="parent">
        <div>
            <input type="text" ng-model="count"/>
        </div>
        <my-name show-name="increment()">
        
        </my-name>
    </div>
</body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller("parent", function($scope){
        $scope.count = 0;
        $scope.increment = function(){
            $scope.count++;
        };
    }).directive("myName", function(){
        return {
            restrict:"EA",
            scope:{
                showName: '&'
            },
            template:'<input type="button" ng-click="showName()" value="+1"/>'
        }
    });
</script>
</html>

### Angular 中 `@ViewChild` 的使用方法 #### 获取组件或指令引用 `@ViewChild` 装饰器允许父组件获取其视图内定义的子组件或指令的实例。这可以通过指定目标的选择器来完成,可以是类型、字符串或其他。 ```typescript import { Component, ViewChild, AfterViewInit } from &#39;@angular/core&#39;; import { ChildComponent } from &#39;./child/child.component&#39;; @Component({ selector: &#39;app-parent&#39;, template: `&lt;app-child #childRef&gt;&lt;/app-child&gt;` }) export class ParentComponent implements AfterViewInit { @ViewChild(&#39;childRef&#39;) child!: ChildComponent; ngAfterViewInit() { console.log(this.child); // 访问到子组件实例 } } ``` 上述代码展示了如何通过模板变量名 `#childRef` 来选取子组件,并将其赋给 `ParentComponent` 类中的 `child` 属性,在生命周期钩子 `ngAfterViewInit` 后可安全访问此属性[^1]。 #### 使用 ElementRef 操作 DOM 元素 除了获取组件外,还可以利用 `ElementRef` 对象直接操纵 HTML 元素: ```typescript import { Component, ViewChild, ElementRef, AfterViewInit } from &#39;@angular/core&#39;; @Component({ selector: &#39;app-root&#39;, template: `&lt;button #myButton&gt;Click me&lt;/button&gt;` }) export class AppComponent implements AfterViewInit { @ViewChild(&#39;myButton&#39;, { read: ElementRef }) button!: ElementRef; ngAfterViewInit() { this.button.nativeElement.style.backgroundColor = &#39;lightblue&#39;; } } ``` 这里设置了 `{ read: ElementRef }` 参数告诉 Angular 返回的是一个 `ElementRef` 实例而不是默认的行为。接着可以在 `ngAfterViewInit` 方法里修改按钮样式[^4]。 #### Static vs Dynamic 查询模式 当声明 `@ViewChild` 时可以选择静态还是动态查询方式,取决于是否设置 `static` 参数为 `true` 或者保持默认值 `false`: - 当 `static=true` ,表示希望在首次渲染前就获得元素; - 默认情况下 (`static=false`) 则是在整个视图构建完成后才可用这些元素。 对于上面提到的例子来说,如果想要尽早拿到子组件,则应如此编写: ```typescript @ViewChild(ChildComponent, { static: true }) private childComp!: ChildComponent; ``` 而如果是基于事件或者其他逻辑条件加载的内容则应该保留默认配置[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值