Angularjs如何在$ scope中包含?

use*_*347 2 javascript angularjs

我想知道是否有可能知道$ scope中是否有一个字符,例如像这样的字符,但是在angularJs中

$scope.test = "Hi, How Are You?";
if($scope.test.Contains("?")) {
  return true
} else {
  return false
}
Run Code Online (Sandbox Code Playgroud)

Sho*_*omz 19

您可以替换Containsmatch:

if($scope.test.match("?")) {
    return true
} else {
    return false
}
Run Code Online (Sandbox Code Playgroud)

您甚至可以进一步优化整个代码块:

return !!$scope.test.match("?");
Run Code Online (Sandbox Code Playgroud)


rye*_*lar 5

好吧,你可以这样做..

var string = "hello";
alert(string.indexOf("llo") != -1);
Run Code Online (Sandbox Code Playgroud)

所以关于你上面的代码..

$scope.test= "HI How Are You ?";
alert($scope.test.indexOf("How") != -1); // return true;
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢使用 `~string.indexOf("llo")` ;) (2认同)