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