angular的scopel指令使用详解

php中世界最好的语言
Lepaskan: 2018-03-12 16:59:04
asal
1627 orang telah melayarinya

这次给大家带来angular的scopel指令使用详解,使用angular的scopel指令注意事项有哪些,下面就是实战案例,一起来看一下。

我们来创建一个自定义指令

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="mainCtrl">
        <my-btn></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        myApp.controller(&#39;mainCtrl&#39;,[&#39;$scope&#39;,function($scope){
            $scope.myClass = &#39;primary&#39;;
        }]);
        myApp.directive(&#39;myBtn&#39;,function(){            return {
                template:&#39;<input type="button" value="按钮" class="{{myClass}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk


1.png

使用自定义指令像上面一样的确不错,但是如果你想要对每一个指令渲染出来的按钮定制化,则好像不可以,比如下面我们创建一堆这个自定义指令,他们长得一模一样:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="mainCtrl">
        <my-btn></my-btn>
        <my-btn></my-btn>
        <my-btn></my-btn>
        <my-btn></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        myApp.controller(&#39;mainCtrl&#39;,[&#39;$scope&#39;,function($scope){
            $scope.myClass = &#39;primary&#39;;
        }]);
        myApp.directive(&#39;myBtn&#39;,function(){            return {
                template:&#39;<input type="button" value="按钮" class="{{myClass}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk


2.png

一种思路是把这几个自定义的指令按钮放到不同的控制器里面,然后控制器里通过$scope上下文传递不同的值:


  <!DOCTYPE html><html><head>
   <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: green;
        }        .default{            background: gray;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="aCtrl">
        <my-btn></my-btn>
    </div>
    <div ng-controller="bCtrl">
        <my-btn></my-btn>
    </div>
    <div ng-controller="cCtrl">
        <my-btn></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        myApp.controller(&#39;aCtrl&#39;,[&#39;$scope&#39;,function($scope){
            $scope.myClass = &#39;primary&#39;;
        }]);
        myApp.controller(&#39;bCtrl&#39;,[&#39;$scope&#39;,function($scope){
            $scope.myClass = &#39;success&#39;;
        }]);
        myApp.controller(&#39;cCtrl&#39;,[&#39;$scope&#39;,function($scope){
            $scope.myClass = &#39;default&#39;;
        }]);
        myApp.directive(&#39;myBtn&#39;,function(){            return {
                template:&#39;<input type="button" value="按钮" class="{{myClass}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

3.png

这样写太麻烦了,所以我们的angular为我们的自定义指令提供了一个配置项叫scope,所以,我们可以如下这样写:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: green;
        }        .default{            background: gray;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn b="className1"></my-btn>
      <my-btn b="className2"></my-btn>
      <my-btn b="className3"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.className1 = &#39;primary&#39;;
          $scope.className2 = &#39;success&#39;;
          $scope.className3 = &#39;default&#39;;
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    a:&#39;=b&#39;
                },
                template:&#39;<input type="button" value="按钮" class="{{a}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

要看懂上面的只要注意两点:

这里的独立作用域里面的a代表的是template里面的模型a

=b代表的是要angular去寻找视图里面的当前指令的属性b

属性b的值需要去外部作用域里面去寻找

如果你想在指令作用域里绑定的模型的名字和外部使用的时候的属性名一样,可以省写成如下:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: green;
        }        .default{            background: gray;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn a="className1"></my-btn>
      <my-btn a="className2"></my-btn>
      <my-btn a="className3"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.className1 = &#39;primary&#39;;
          $scope.className2 = &#39;success&#39;;
          $scope.className3 = &#39;default&#39;;
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    a:&#39;=&#39;
                },
                template:&#39;<input type="button" value="按钮" class="{{a}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

当然,上面的=号是双向数据绑定:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: green;
        }        .default{            background: gray;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn a="abc"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.abc = &#39;我是初始内容&#39;;
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    a:&#39;=&#39;
                },
                template:&#39;<input type="text"  ng-model="a"><span>{{a}}</span>&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

如果只是想单向的数据通信,可以用@符号:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: red;
        }        .default{            background: red;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn a="primary"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.mm = &#39;primary&#39;;
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    a:&#39;@&#39;
                },
                template:&#39;<input type="button" value="按钮" class="{{a}}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

如果想用ng-class,也是可以的:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: red;
        }        .default{            background: red;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn a="primary"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.mm = true;
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    a:&#39;@&#39;
                },
                template:&#39;<input type="button" value="按钮" ng-class="{primary:a}">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

最后,还有一个scope可以设置是引用外部作用域的方法

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .primary{            background: red;
        }        .success{            background: red;
        }        .default{            background: red;
        }    </style></head><body ng-app="myApp">
    <div ng-controller="Controller">
      <my-btn fn2="fn()"></my-btn>
    </div>
    <script src="node_modules/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module(&#39;myApp&#39;,[]);
        
        myApp
        .controller(&#39;Controller&#39;, [&#39;$scope&#39;, function($scope) {
          $scope.fn = function(){
            alert(11);
          }
        }])
        .directive(&#39;myBtn&#39;,function(){            return {
                scope:{
                    fn1:&#39;&fn2&#39;
                },
                template:&#39;<input type="button" value="按钮" ng-click="fn1()">&#39;
            }
        });    </script></body></html>
Salin selepas log masuk

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Angular Material的使用详解

css中id选择器的命名规则有哪些

让元素水平垂直居中冷门方法

Atas ialah kandungan terperinci angular的scopel指令使用详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan