最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - checking string value in ng-if statement - Stack Overflow

programmeradmin2浏览0评论

I would like to check the value of the property of an object and would like to check the data of string to compare.

<div ng-if="results.dataType === 'textTable'"> 
     This text belongs in a table. 
</div>

So far all the divs appear with the text in the body where only two divs should display it.

Is there anything wrong with my ng-if statement and string comparison?

I would like to check the value of the property of an object and would like to check the data of string to compare.

<div ng-if="results.dataType === 'textTable'"> 
     This text belongs in a table. 
</div>

So far all the divs appear with the text in the body where only two divs should display it.

Is there anything wrong with my ng-if statement and string comparison?

Share Improve this question edited Aug 22, 2017 at 16:19 Ben Racicot 5,93416 gold badges80 silver badges139 bronze badges asked May 25, 2017 at 7:32 bluePearlbluePearl 1,2575 gold badges26 silver badges44 bronze badges 4
  • If this is AngularJS (sometimes called "Angular 1"), then the correct tag is "angularjs". – user663031 Commented May 25, 2017 at 7:53
  • The way I see it from your code is that you have a object with a value which you compare it with a string. So supposing that you are wrapping the ng-if in a ng-for, to generate multiple divs you should have an array with result values specific for each new created div to actually work that ng-if – sTx Commented May 25, 2017 at 14:13
  • @torazaburo this is in angular 2 – bluePearl Commented May 25, 2017 at 16:08
  • @sTx yes my ng-if is wrapped in a ngFor results is the returned items array that contains the data but my if statements doesn't seem to work but not sure why. – bluePearl Commented May 25, 2017 at 16:10
Add a comment  | 

3 Answers 3

Reset to default 6

Here is the demo Jsfiddle

Js code

  var app = angular.module('myApp', []);

  app.controller('ctrl', function($scope) {
    $scope.results = {
      dataType: 'textTable'
    };
    $scope.flag = true;

    // for testing purpose

    $scope.toggle = function() {
      if ($scope.flag) {
        $scope.results = {
          dataType: 'textTable'
        };
        $scope.flag = !$scope.flag;
      } else {
        $scope.results = {
          dataType: 'textTableNot'
        };
        $scope.flag = !$scope.flag;
      }

    }
  });

HTML

  <div ng-app='myApp'>

    <div ng-controller='ctrl'>
      <div ng-if='results.dataType === "textTable"'> This text belongs in a table.</div>
      {{results.dataType}}
      <button ng-click='toggle()'>
        Toggle
      </button>
    </div>
  </div>

Hope this will resolve your problem

I realized that in angular 2 the if statement is: *ngIf and not ng-if.

Hope this will resolve your problem.

<div>
<input type="hidden" ng-model="myVar" ng-init="myVar = 'stringformat'">
<div ng-if="myVar =='stringformat'">
<h1>Welcome</h1>
<p>Welcome to my home.</p>
</div>
</div>
发布评论

评论列表(0)

  1. 暂无评论