:
如果video.yt $ aspectRatio等于宽屏那么 元素的attr height =“270px”其他 element的attr height =“360px”
我正在使用ng-repeat迭代视频。没有 理念 </跨度> 我应该怎么做这个条件:
在范围中添加功能?在模板中做到这一点?
…
Angularjs(1.1.5以下版本)不提供 if/else 功能。以下是您要实现的目标的几个选项:
if/else
( 的 如果您使用的是1.1.5或更高版本,请跳至下面的更新(#5) 强> )
正如@Kirk在评论中所建议的那样,最简单的方法是使用三元运算符,如下所示:
<span>{{isLarge ? 'video.large' : 'video.small'}}</span>
ng-switch
可以使用类似以下的东西。
<div ng-switch on="video"> <div ng-switch-when="video.large"> <!-- code to render a large video block--> </div> <div ng-switch-default> <!-- code to render the regular video block --> </div> </div>
ng-hide
ng-show
或者,您也可以使用 ng-show/ng-hide 但使用它实际上会渲染一个大视频和一个小视频元素,然后隐藏符合的视频元素 ng-hide 条件并显示遇到的那个 ng-show 条件。因此,在每个页面上,您实际上将呈现两个不同的元素。
ng-show/ng-hide
ng-class
这可以如下使用。
<div ng-class="{large-video: video.large}"> <!-- video block goes here --> </div>
以上基本上都会添加一个 large-video css类为div元素if video.large 是真的。
large-video
video.large
ngIf directive
ng-if
在上面的版本中 1.1.5 你可以使用 ng-if 指示。如果提供的表达式返回,这将删除元素 false 并重新插入 element 在表达式返回的DOM中 true 。可以如下使用。
1.1.5
false
element
true
<div ng-if="video == video.large"> <!-- code to render a large video block--> </div> <div ng-if="video != video.large"> <!-- code to render the regular video block --> </div>
你可以使用你的 video.yt$aspectRatio 直接通过过滤器传递属性,并将结果绑定到模板中的height属性。
video.yt$aspectRatio
您的过滤器看起来像:
app.filter('videoHeight', function () { return function (input) { if (input === 'widescreen') { return '270px'; } else { return '360px'; } }; });
模板将是:
<video height={{video.yt$aspectRatio | videoHeight}}></video>
我同意三元非常干净。似乎它是非常情境化的,虽然我需要显示div或p或table,所以使用表格我不喜欢三元组,原因很明显。调用函数通常是理想的,或者在我的情况下,我这样做:
<div ng-controller="TopNavCtrl"> <div ng-if="info.host ==='servername'"> <table class="table"> <tr ng-repeat="(group, status) in user.groups"> <th style="width: 250px">{{ group }}</th> <td><input type="checkbox" ng-model="user.groups[group]" /></td> </tr> </table> </div> <div ng-if="info.host ==='otherservername'"> <table class="table"> <tr ng-repeat="(group, status) in user.groups"> <th style="width: 250px">{{ group }}</th> <td><input type="checkbox" ng-model="user.groups[group]" /></td> </tr> </table> </div> </div>
Angular本身不提供if / else功能,但您可以通过包含此模块来获取它:
https://github.com/zachsnow/ng-elif
用它自己的话来说,它只是“一个简单的控制流指令集合:ng-if,ng-else-if和ng-else。”它使用起来简单直观。
例:
<div ng-if="someCondition"> ... </div> <div ng-else-if="someOtherCondition"> ... </div> <div ng-else> ... </div>
<div ng-if="modeldate==''"><span ng-message="required" class="change">Date is required</span> </div>
您可以使用上面的ng-if指令。
Angular的可能性: 我必须在html部分包含一个if语句,我必须检查我生成的URL的所有变量是否都已定义。我是通过以下方式完成的,它似乎是一种灵活的方法。我希望它会对某些人有所帮助。
模板中的html部分:
<div *ngFor="let p of poemsInGrid; let i = index" > <a [routerLink]="produceFassungsLink(p[0],p[3])" routerLinkActive="active"> </div>
打字稿部分:
produceFassungsLink(titel: string, iri: string) { if(titel !== undefined && iri !== undefined) { return titel.split('/')[0] + '---' + iri.split('raeber/')[1]; } else { return 'Linkinformation has not arrived yet'; } }
谢谢和最好的问候,
一月
三元是最明确的做法。
<div>{{ConditionVar ? 'varIsTrue' : 'varIsFalse'}}</div>