问题描述
我有以下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.
- 如果干草堆中没有 needle ,则返回 -1 .
- 如果干草堆中存在 needle ,则返回 0 .
- 返回 needle 所在的索引.
- If the needle is not present in the haystack -1 is returned.
- If needle is present at the beginning of the haystack 0 is returned.
- 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如果字符串包含角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!