2016-09-27 98 views
1

我能确定一个角指令,以便它匹配多个类似的术语 即角指令来匹配多个属性

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

将匹配以下两个:

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

您可以设置'

“,然后只需链接'平台terms'或'平台suggestions'在指令 – thepio

回答

2

没有通配符指令宣言。

但是你可以隔离功能,重复定义:

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


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

您可以创建的指令,它可以让你在指令隔离范围使用这些属性隔离范围。就像这样:

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>

+0

这是一个有效的解决方案,但我。宁愿保留HTML清洁器 –

+0

是的。在其他情况下可能更适合。 – thepio