Angular指令匹配多个属性

时间:2016-09-27 11:22:44

标签: javascript angularjs

我可以定义一个角度指令,使其匹配多个相似的术语 即

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

将匹配以下内容:

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

2 个答案:

答案 0 :(得分:2)

没有通配符指令声明。

但您可以隔离该功能并重复定义:

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


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

答案 1 :(得分:1)

您可以为指令创建隔离范围,以允许您在指令的隔离范围中使用这些属性。像这样:

<!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>
// this will redirect all NSLog in your project, add it to PCH file.
#define NSLog(args...) writeLog(__PRETTY_FUNCTION__, __LINE__, args)

void writeLog(const char *function, int lineNumber, NSString *format, ...) {

// basic log content.
va_list ap;
va_start (ap, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:ap];   
va_end (ap);

// add function name, and line number.
NSString *log = [NSString stringWithFormat:@"%s line %d $ %@", function, lineNumber, msg];

// add your own code to write `log` into a text file.
....
}