我想知道除了使用 ng-show 等之外,是否有一种角度有条件地显示内容的方法。例如,在backbone.js 中,我可以在模板中对内联内容做一些事情,例如:
<% if (myVar === "two") { %> show this<% } %>
但是在角度上,我似乎仅限于显示和隐藏包含在 html 标签中的东西
<p ng-hide="true">I'm hidden</p>
<p ng-show="true">I'm shown</p>
Angular 中推荐的方法是仅使用 {{}} 有条件地显示和隐藏 Angular 中的内联内容,而不是将内容包装在 html 标记中?
编辑:下面 2Toad 的答案就是您要找的!点赞那个东西
如果您使用的是 Angular <= 1.1.4,那么这个答案就可以了:
对此还有一个答案。我发布了一个单独的答案,因为它更像是对解决方案的“精确”尝试,而不是可能的解决方案列表:
这是一个将执行“立即 if”(又名 iif)的过滤器:
app.filter('iif', function () {
return function(input, trueValue, falseValue) {
return input ? trueValue : falseValue;
};
});
并且可以像这样使用:
{{foo == "bar" | iif : "it's true" : "no, it's not"}}
成千上万种剥这只猫的方法。我知道您在 {{}} 之间特别询问,但对于来到这里的其他人,我认为值得展示其他一些选项。
在您的 $scope 上运行(IMO,这是您在大多数情况下的最佳选择):
app.controller('MyCtrl', function($scope) {
$scope.foo = 1;
$scope.showSomething = function(input) {
return input == 1 ? 'Foo' : 'Bar';
};
});
<span>{{showSomething(foo)}}</span>
ng-show 和 ng-hide 当然:
<span ng-show="foo == 1">Foo</span><span ng-hide="foo == 1">Bar</span>
<div ng-switch on="foo">
<span ng-switch-when="1">Foo</span>
<span ng-switch-when="2">Bar</span>
<span ng-switch-default>What?</span>
</div>
Bertrand 建议的自定义过滤器。 (如果您必须一遍又一遍地做同样的事情,这是您的最佳选择)
app.filter('myFilter', function() {
return function(input) {
return input == 1 ? 'Foo' : 'Bar';
}
}
{{foo | myFilter}}
或自定义指令:
app.directive('myDirective', function() {
return {
restrict: 'E',
replace: true,
link: function(scope, elem, attrs) {
scope.$watch(attrs.value, function(v) {
elem.text(v == 1 ? 'Foo': 'Bar');
});
}
};
});
<my-directive value="foo"></my-directive>
就个人而言,在大多数情况下,我会在我的范围内使用一个函数,它可以保持标记非常干净,并且实现起来又快又容易。除非,也就是说,你将一遍又一遍地做同样的事情,在这种情况下,我会按照 Bertrand 的建议,根据具体情况创建一个过滤器或可能的指令。
与往常一样,最重要的是您的解决方案易于维护,并且希望是可测试的。这将完全取决于您的具体情况。
当无法使用 ng-class 时(例如在设置 SVG 样式时),我使用以下有条件地设置类 attr:
ng-attr-class="{{someBoolean && 'class-when-true' || 'class-when-false' }}"
相同的方法应该适用于其他属性类型。
(我认为您需要使用最新的不稳定 Angular 才能使用 ng-attr-,我目前使用的是 1.1.4)
我发表了一篇关于使用 AngularJS+SVG 的文章,讨论了这个问题和相关问题。 http://www.codeproject.com/Articles/709340/Implementing-a-Flowchart-with-SVG-and-AngularJS
要检查变量内容并具有默认文本,您可以使用:
<span>{{myVar || 'Text'}}</span>
如果我很好理解你,我认为你有两种方法可以做到这一点。
首先您可以尝试 ngSwitch,第二种可能的方法是创建您自己的 filter。可能 ngSwitch 是正确的方法,但如果您想隐藏或显示内联内容,只需使用 {{}} 过滤器即可。
下面是一个带有简单过滤器的 fiddle 作为示例。
<div ng-app="exapleOfFilter">
<div ng-controller="Ctrl">
<input ng-model="greeting" type="greeting">
<br><br>
<h1>{{greeting|isHello}}</h1>
</div>
</div>
angular.module('exapleOfFilter', []).
filter('isHello', function() {
return function(input) {
// conditional you want to apply
if (input === 'hello') {
return input;
}
return '';
}
});
function Ctrl($scope) {
$scope.greeting = 'hello';
}
Angular UI 库具有内置指令 ui-if,用于模板/视图中的条件,最高 Angular ui 1.1.4
示例:支持 Angular UI 直到 ui 1.1.4
<div ui-if="array.length>0"></div>
ng-if 在 1.1.4 之后的所有 Angular 版本中都可用
<div ng-if="array.length>0"></div>
如果数组变量中有任何数据,则只会出现 div
如果您想在值为“0”时显示“None”,您可以用作:
<span> {{ $scope.amount === "0" ? $scope.amount : "None" }} </span>
或角度js中的真假
<span> {{ $scope.amount === "0" ? "False" : "True" }} </span>
所以对于 Angular 1.5.1(现有的应用程序依赖于其他一些 MEAN 堆栈依赖项是我目前不使用 1.6.4 的原因)
这对我有用,就像 OP 说的 {{myVar === "two" ? "it's true" : "it's false"}}
{{vm.StateName === "AA" ? "ALL" : vm.StateName}}
即使在异国情调的情况下也能工作:
<br ng-show="myCondition == true" />
我会把我的混在一起:
https://gist.github.com/btm1/6802312
这将评估 if 语句一次并且不添加监视侦听器但是您可以向具有 set-if 的元素添加一个附加属性,称为 wait-for="somedata.prop" 并且它将等待该数据或属性在之前设置评估 if 语句一次。如果您正在等待来自 XHR 请求的数据,那么该附加属性会非常方便。
angular.module('setIf',[]).directive('setIf',function () {
return {
transclude: 'element',
priority: 1000,
terminal: true,
restrict: 'A',
compile: function (element, attr, linker) {
return function (scope, iterStartElement, attr) {
if(attr.waitFor) {
var wait = scope.$watch(attr.waitFor,function(nv,ov){
if(nv) {
build();
wait();
}
});
} else {
build();
}
function build() {
iterStartElement[0].doNotMove = true;
var expression = attr.setIf;
var value = scope.$eval(expression);
if (value) {
linker(scope, function (clone) {
iterStartElement.after(clone);
clone.removeAttr('set-if');
clone.removeAttr('wait-for');
});
}
}
};
}
};
});
我认为最近的角度,6+。您有带有 else 条件的 ng-template 连接到标签标识符:
<div *ngIf="myVar else myVarNo">Yes</div>
<ng-template #myVarNo><div>No</div></ng-template>
不定期副业成功案例分享
myVar
的值? (即如何在表达式中嵌套变量?)我尝试使用{{myVar === "two" ? "it's true" : {{myVar}}}}
但它不起作用。myVar
属性不需要用额外的花括号括起来,它已经在表达式中。试试{{myVar === "two" ? "it's true" : myVar}}