Javascript ng 如果字符串的角度包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30869370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:52:04  来源:igfitidea点击:

ng if with angular for string contains

javascriptjquerycssarraysangularjs

提问by Ptheitroado

I have the following Angular code:

我有以下角度代码:

<li ng-repeat="select in Items">   
         <foo ng-repeat="newin select.values">
{{new.label}}</foo>

How can I use an ng-if condition to look for a specific character:

如何使用 ng-if 条件查找特定字符:

ng-if="select.name == '?'" 

to only display the code when the character is here? The value I have is like 88?77 and the numbers are dynamic but the question mark is always there, I can't seem to filter based on that?

只在角色出现时显示代码?我拥有的值就像 88?77 并且数字是动态的,但问号总是存在,我似乎无法根据它进行过滤?

回答by Tushar

ES2015 UPDATE

ES2015 更新

ES2015 have String#includesmethod that checks whether a string contains another. This can be used if the target environment supports it. The method returns trueif the needleis found in haystackelse returns false.

ES2015 有String#includes检查一个字符串是否包含另一个字符串的方法。如果目标环境支持它,则可以使用它。true如果在haystack 中找到针,则该方法返回,否则返回。false

ng-if="haystack.includes(needle)"

Here, needleis the string that is to be searched in haystack.

这里,needle是要在 中搜索的字符串haystack

See Browser Compatibilitytable from MDN. Note that this is not supported by IE and Opera. In this case polyfillcan be used.

请参阅MDN 中的浏览器兼容性表。请注意,IE 和 Opera 不支持此功能。在这种情况下,可以使用polyfill



You can use String#indexOfto get the index of the needlein haystack.

您可以使用String#indexOf获得的指数大海捞针

  1. If the needleis not present in the haystack-1is returned.
  2. If needleis present at the beginning of the haystack0is returned.
  3. Else the index at which needleis, is returned.
  1. 如果不在干草堆中,则返回-1
  2. 如果haystack的开头有,则返回0
  3. 否则返回指针所在的索引。

The index can be compared with -1to check whether needleis found in haystack.

可以与该索引进行比较-1以检查是否在haystack 中找到了

ng-if="haystack.indexOf(needle) > -1" 

回答by Mathew Berg

ng-if="select.name.indexOf('?') !== -1" 

回答by Shubham Nigam

All javascript methods are applicable with angularjs because angularjs itself is a javascript framework so you can use indexOf() inside angular directives

所有 javascript 方法都适用于 angularjs,因为 angularjs 本身是一个 javascript 框架,因此您可以在 angular 指令中使用 indexOf()

<li ng-repeat="select in Items">   
         <foo ng-repeat="newin select.values">
<span ng-if="newin.label.indexOf(x) !== -1">{{newin.label}}</span></foo>
</li>
//where x is your character to be found

回答by Ruan

Only the shortcut syntax worked for me *ngIf.

只有快捷语法对我有用 *ngIf

(I think it's the later versions that use this syntax if I'm not mistaken)

(如果我没记错的话,我认为是使用这种语法的更高版本)

<div *ngIf="haystack.indexOf('needle') > -1">
</div>

or

或者

<div *ngIf="haystack.includes('needle')">
</div>

回答by LionC

Do checks like that in a controller function. Your HTML should be easy-to-read markup without logic.

在控制器函数中做这样的检查。您的 HTML 应该是没有逻辑的易于阅读的标记。

Controller:

控制器:

angular.module("myApp")
.controller("myController",function(){
    var self = this;

    self.select = { /* ... */ };

    self.showFoo = function() {
        //Checks if self.select.name contains the character '?'
        return self.select.name.indexOf('?') != -1;
    }
});

Page example:

页面示例:

<div ng-app="myApp" ng-controller="myController as vm">
    <p ng-show="vm.showFoo()">Bar</p>
</div>