angularJS自定义属性作为条件中转


<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<body ng-app="components">
<div ng-controller="c1">
    <myelement alert="loadData1()">
        <h1>哈哈</h1>
    </myelement>
</div>
<div ng-controller="c2">
    <myelement alert="loadData2()">
        <h1>吼吼</h1>
    </myelement>
</div>
</body>
<script src="angular.js"></script>
<script>
    var app= angular.module('components', []);
    app.controller("c1",function($scope){
        $scope.loadData1=function(){
            alert("哈哈");
        }
    });

    app.controller("c2",function($scope){
        $scope.loadData2=function(){
            alert("吼吼");
        }
    });

    app.directive("myelement",function(){
        return {
            link:function(scope, element, attr, superCtrl){
                element.bind("click",function(){
                    scope.$apply(attr.alert);//在这里注意下如果自定义属性使用驼峰命名法,那么attr调用该属性时仍然要保证完全小写
                });
            }
        }
    });
</script>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM