有没有什么方法可以说像这样的角度:
<th ng-repeat=" o in Odds" >{{o.Name || "-"}}</th>
Run Code Online (Sandbox Code Playgroud)
那么如果o.Name中没有数据显示" - "?
您的示例应该可以工作,但是如果您在o.Name中有空格或函数,它将无法解析为falsey,您将在HTML中获得一个不可见的空格而不是所需的短划线.
可以使用通用过滤器将空值替换为破折号,并首先对输入应用各种规范化:
angular.module('App.filters', []).filter('placeholder', [function () {
return function (text, placeholder) {
// If we're dealing with a function, get the value
if (angular.isFunction(text)) text = text();
// Trim any whitespace and show placeholder if no content
return text.trim() || placeholder;
};
}]);
Run Code Online (Sandbox Code Playgroud)
然后您可以按如下方式使用它:
<th ng-repeat=" o in Odds" >{{o.Name | placeholder:'-'}}</th>
Run Code Online (Sandbox Code Playgroud)
然后,这可以完全重用于其他行/列以及您要应用相同规则的任何其他位置.
示例:http://jsfiddle.net/kfknbsp7/4/