ng如果字符串包含角

ng如果字符串包含角

本文介绍了ng如果字符串包含角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Angular代码:

I have the following Angular code:

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

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

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

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

仅在字符在此处时显示代码?我的值是88?77,数字是动态的,但是问号总是存在,我似乎无法基于此进行过滤?

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?

推荐答案

ES2015更新

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

ES2015 have String#includes method that checks whether a string contains another. This can be used if the target environment supports it. The method returns true if the needle is found in haystack else returns false.

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

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

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

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

See Browser Compatibility table from MDN. Note that this is not supported by IE and Opera. In this case polyfill can be used.

您可以使用 String#indexOf 获取干草堆 needle 的索引.

You can use String#indexOf to get the index of the needle in haystack.

  1. 如果干草堆中没有 needle ,则返回 -1 .
  2. 如果干草堆中存在 needle ,则返回 0 .
  3. 返回 needle 所在的索引.
  1. If the needle is not present in the haystack -1 is returned.
  2. If needle is present at the beginning of the haystack 0 is returned.
  3. Else the index at which needle is, is returned.

可以将索引与-1进行比较,以检查是否在干草堆中找到了 needle .

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

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

这篇关于ng如果字符串包含角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:52