angularjs if statements?

后端 未结 8 1775
暖寄归人
暖寄归人 2021-01-04 20:06

So I\'m running through the tutorial for AngularJS:

I have an array defined in the controller and i\'m returning different points in the array by calling when i\'m l

相关标签:
8条回答
  • 2021-01-04 20:06

    I think the answer needs an update.

    Previously you could use ngIf directive from AngularUI project (code here if you still want to download it), bad news is that it's not maintained any more.

    The good news is that it has been added to the official AngularJS repo (unstable branch) and soon will be available in the stable one.

    <div ng-if="something"> Foo bar </div>
    

    Will not just hide the DIV element, but remove it from DOM as well (when something is falsy).

    0 讨论(0)
  • 2021-01-04 20:13

    I do not think there is if statement available. For your styling purpose, ng-class can be used.

    <p ng-class="{important: feature.importance == 10 }">
    

    ng-switch is also convenient.

    -- update --

    take a look at: https://stackoverflow.com/a/18021855/1238847

    angular1.2.0RC seems to have ng-if support.

    0 讨论(0)
  • 2021-01-04 20:17

    ng-class is probably the best answer to your issue, but AngularUI has an "if" directive:

    http://angular-ui.github.com/

    search for: Remove elements from the DOM completely instead of just hiding it.

    I used "ui-if" to decide if I should render a data value as a label or an input, relative to the current month:

    <tbody id="allocationTableBody">
        <tr ng-repeat="a in data.allocations">
            <td>{{a.monthAbrv}}</td>
            <td ui-if="$index < currentMonth">{{a.amounts[0]}}</td>
        </tr>
    </tbody>
    
    0 讨论(0)
  • 2021-01-04 20:23

    What also works is:

    <span>{{ varWithValue || 'If empty use this string' }}</span> 
    
    0 讨论(0)
  • 2021-01-04 20:24

    Actually there is a ternary operator in Angular 1.2.0.

    <p style="{{feature.importance == 10 ? 'color:red' : ''}}">{{feature.description}}</p>
    
    0 讨论(0)
  • 2021-01-04 20:26

    You can also try this line of code below

    <div class="{{is_foo && foo.bar}}">
    

    which shows foo.bar if is_foo is true.

    0 讨论(0)
提交回复
热议问题