好好爱自己!

angular directive 深入理解

由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解。

感觉才开始真正理解了这句话的意思:

In an AngularJS directive the scope allows you to access the data in the attributes of the element to which the directive is applied

这句话,感觉道出了diretive的原理的精髓。

--------------------------------------------------------------------------------------------------------------------

> is not in the documentation.

< is for one-way binding.

@ binding is for passing strings. These strings support {{}} expressions for interpolated values.

= binding is for two-way model binding. The model in parent scope is linked to the model in the directive's isolated scope.

& binding is for passing a method into your directive's scope so that it can be called within your directive.

When we are setting scope: true in directive, Angular js will create a new scope for that directive. That means any changes made to the directive scope will not reflect back in parent controller.

 

---------------------------------------------------------------------------------------------------------------------

In an AngularJS directive the scope allows you to access the data in the attributes of the element to which the directive is applied.

This is illustrated best with an example:

<div my-customer name="Customer XYZ"></div>

and the directive definition:

angular.module('myModule', [])
.directive('myCustomer', function() {
  return {
    restrict: 'E',
    scope: {
      customerName: '@name'
    },
    controllerAs: 'vm',
    bindToController: true,
    controller: ['$http', function($http) {
      var vm = this;

      vm.doStuff = function(pane) {
        console.log(vm.customerName);
      };
    }],
    link: function(scope, element, attrs) {
      console.log(scope.customerName);
    }
  };
});

When the scope property is used the directive is in the so called "isolated scope" mode, meaning it can not directly access the scope of the parent controller.

In very simple terms, the meaning of the binding symbols is:

someObject: '=' (two-way data binding)

someString: '@' (passed directly or through interpolation with double curly braces notation {{}})

someExpression: '&' (e.g. hideDialog())

This information is present in the AngularJS directive documentation page, although somewhat spread throughout the page.

The symbol > is not part of the syntax.

However, < does exist as part of the AngularJS component bindings and means one way binding.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE>
<html ng-app="App">
    <head>
        <meta charset="utf-8"/>
        <script src="./js/angular.min.js"></script>
        <script>
            var app = angular.module('App', []);
            app.controller('appCtrl', function($scope){
                // $scope.names = {age: 12};
                $scope.names = 'FLY';
                // DB.getAl().then(function (rsp) {
                 //    $scope.name=100;
                // })
                $scope.changeNames = function(){
                    $scope.names = "AAA";
                };
                $scope.cbFun = function(name){
                    // alert('parent action.');
                    alert(name);
                    /**
                     *
                     *
                     *
                     */
                };
                $scope.doStuff = function () {
                    alert('parent scope');
                }
 
            });
            app.directive('myCustomer', function(){
                return {
                    scope: {
                        fly: '@'
                    },
                    restrict: 'A',
                    link: function(scope, element, attr){
                        console.log('attr: ', attr);
 
                        console.log("myCustomer: ", scope);
                    },
                    controllerAs: 'vmst',
                    bindToController: true,
                    controller: ['$scope', '$http', function(scope, $http) {
                        var vm = this;
                        console.log('this: ', this);
                        console.log('scope: ', scope);
                        vm.doStuff = function(pane) {
                            console.log(vm.customerName);
                        };
                        vm.name = 'pxk';
                        scope.doStuff = function(){
                            alert('asdfafa');
                        }
                    }],
                    // template: '<button ng-click="doStuff();">aaaa</button>'
                };
            });
 
        </script>
    </head>
    <body ng-controller="appCtrl">
        <div my-customer="" haha="12ab3" fly="jiayou">myCustomer
 
            <button ng-click="doStuff();">aaa</button>
        </div>
        <hr/>
    </body>
</html>

  

posted @   立志做一个好的程序员  阅读(417)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2017-06-29 golang time.Duration()的问题解疑
2016-06-29 php 中的全局变量的理解

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示