inline conditionals in angular.js

后端 未结 11 1070
Happy的楠姐
Happy的楠姐 2020-11-28 01:37

I was wondering if there is a way in angular to conditionally display content other than using ng-show etc. For example in backbone.js I could do something with inline conte

相关标签:
11条回答
  • 2020-11-28 02:09

    Angular 1.1.5 added support for ternary operators:

    {{myVar === "two" ? "it's true" : "it's false"}}
    
    0 讨论(0)
  • 2020-11-28 02:09

    Works even in exotic Situations:

    <br ng-show="myCondition == true" />
    
    0 讨论(0)
  • 2020-11-28 02:15

    For checking a variable content and have a default text, you can use:

    <span>{{myVar || 'Text'}}</span>
    
    0 讨论(0)
  • 2020-11-28 02:16

    Thousands of ways to skin this cat. I realize you're asking about between {{}} speifically, but for others that come here, I think it's worth showing some of the other options.

    function on your $scope (IMO, this is your best bet in most scenarios):

      app.controller('MyCtrl', function($scope) {
          $scope.foo = 1;
    
          $scope.showSomething = function(input) {
               return input == 1 ? 'Foo' : 'Bar';
          };
       });
    
     <span>{{showSomething(foo)}}</span>
    

    ng-show and ng-hide of course:

     <span ng-show="foo == 1">Foo</span><span ng-hide="foo == 1">Bar</span>
    

    ngSwitch

     <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>
    

    A custom filter as Bertrand suggested. (this is your best choice if you have to do the same thing over and over)

    app.filter('myFilter', function() {
       return function(input) {
         return input == 1 ? 'Foo' : 'Bar';
       }
    }
    
    {{foo | myFilter}}
    

    Or A custom directive:

    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>
    

    Personally, in most cases I'd go with a function on my scope, it keeps the markup pretty clean, and it's quick and easy to implement. Unless, that is, you're going to be doing the same exact thing over and over again, in which case I'd go with Bertrand's suggestion and create a filter or possibly a directive, depending on the circumstances.

    As always, the most important thing is that your solution is easy to maintain, and is hopefully testable. And that is going to depend completely on your specific situation.

    0 讨论(0)
  • 2020-11-28 02:18

    If I understood you well I think you have two ways of doing it.

    First you could try ngSwitch and the second possible way would be creating you own filter. Probably ngSwitch is the right aproach but if you want to hide or show inline content just using {{}} filter is the way to go.

    Here is a fiddle with a simple filter as an example.

    <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';
    }
    
    0 讨论(0)
提交回复
热议问题