温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

angular中scopel指令怎么用

发布时间:2021-01-30 13:41:22 来源:亿速云 阅读:132 作者:小新 栏目:web开发

这篇文章将为大家详细讲解有关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('myApp',[]);
        myApp.controller('mainCtrl',['$scope',function($scope){
            $scope.myClass = 'primary';
        }]);
        myApp.directive('myBtn',function(){            return {
                template:'<input type="button" value="按钮" class="{{myClass}}">'
            }
        });    </script></body></html>

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>
        <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('myApp',[]);
        myApp.controller('mainCtrl',['$scope',function($scope){
            $scope.myClass = 'primary';
        }]);
        myApp.directive('myBtn',function(){            return {
                template:'<input type="button" value="按钮" class="{{myClass}}">'
            }
        });    </script></body></html>


angular中scopel指令怎么用

一种思路是把这几个自定义的指令按钮放到不同的控制器里面,然后控制器里通过$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('myApp',[]);
        myApp.controller('aCtrl',['$scope',function($scope){
            $scope.myClass = 'primary';
        }]);
        myApp.controller('bCtrl',['$scope',function($scope){
            $scope.myClass = 'success';
        }]);
        myApp.controller('cCtrl',['$scope',function($scope){
            $scope.myClass = 'default';
        }]);
        myApp.directive('myBtn',function(){            return {
                template:'<input type="button" value="按钮" class="{{myClass}}">'
            }
        });    </script></body></html>

angular中scopel指令怎么用

这样写太麻烦了,所以我们的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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.className1 = 'primary';
          $scope.className2 = 'success';
          $scope.className3 = 'default';
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    a:'=b'
                },
                template:'<input type="button" value="按钮" class="{{a}}">'
            }
        });    </script></body></html>

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

这里的独立作用域里面的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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.className1 = 'primary';
          $scope.className2 = 'success';
          $scope.className3 = 'default';
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    a:'='
                },
                template:'<input type="button" value="按钮" class="{{a}}">'
            }
        });    </script></body></html>

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

<!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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.abc = '我是初始内容';
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    a:'='
                },
                template:'<input type="text"  ng-model="a"><span>{{a}}</span>'
            }
        });    </script></body></html>

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

<!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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.mm = 'primary';
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    a:'@'
                },
                template:'<input type="button" value="按钮" class="{{a}}">'
            }
        });    </script></body></html>

如果想用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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.mm = true;
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    a:'@'
                },
                template:'<input type="button" value="按钮" ng-class="{primary:a}">'
            }
        });    </script></body></html>

最后,还有一个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('myApp',[]);
        
        myApp
        .controller('Controller', ['$scope', function($scope) {
          $scope.fn = function(){
            alert(11);
          }
        }])
        .directive('myBtn',function(){            return {
                scope:{
                    fn1:'&fn2'
                },
                template:'<input type="button" value="按钮" ng-click="fn1()">'
            }
        });    </script></body></html>

关于“angular中scopel指令怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI