簡體   English   中英

Angular指令匹配多個屬性

[英]Angular directive to match multiple attributes

我可以定義一個角度指令,以便它匹配多個相似的術語,即

angular.module('search').directive('platformPreload', function() {
  return {
    link: function(scope, element, attrs) {
    }
  }
}

將匹配以下兩者:

<div platform-preload-terms="[]"></div>
<div platform-preload-suggestions="[]"></div>

沒有通配符指令聲明。

但是你可以隔離函數並重復定義:

angular.module('search')
    .directive('platformPreload', PlatFunction)
    .directive('platformPreloadSuggestions', PlatFunction)


PlatFunction() {
    return {
        link: function(scope, element, attrs) { }
    }
}

您可以為指令創建隔離范圍,以允許您在指令的隔離范圍中使用這些屬性。 像這樣:

 angular.module('myApp', []) .controller('appController', function($scope) { }) .directive('platformPreload', function() { return { restrict: 'A', scope: { platformTerms: '@', platformSuggestions: '@' }, link: function($scope, element, attrs) { console.log('DIRECTIVE'); if ($scope.platformTerms) { console.log($scope.platformTerms); } if ($scope.platformSuggestions) { console.log($scope.platformSuggestions); } } }; }); 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> </head> <body ng-app="myApp" ng-controller="appController"> <div platform-preload platform-terms="These are the terms"></div> <div platform-preload platform-suggestions="These are the suggestions"></div> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM