3

I want to know if it's possible to know if there are a char in a $scope for example something like this but in angularJs

$scope.test = "Hi, How Are You?";
if($scope.test.Contains("?")) {
  return true
} else {
  return false
}
godfrzero
  • 2,230
  • 1
  • 26
  • 31
user3507347
  • 41
  • 1
  • 1
  • 2
  • 3
    AngularJS is *just* a JavaScript framework, so everything that is available for strings in JavaScript is available for strings in AngularJS... – sp00m Apr 07 '14 at 15:15
  • Look at: http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring-in-javascript – drew_w Apr 07 '14 at 15:16
  • 3
    Btw, note that you can replace the whole `if` block of your pseudocode with something like to `return $scope.test.Contains("?")`. No need to return the `true` and `false` values separately like that. – godfrzero Apr 07 '14 at 15:21

3 Answers3

21

You can just replace Contains with match:

if($scope.test.match("?")) {
    return true
} else {
    return false
}

And you can even further optimize this whole code block to just:

return !!$scope.test.match("?");
Shomz
  • 37,421
  • 4
  • 57
  • 85
5

Well you can do it like this..

var string = "hello";
alert(string.indexOf("llo") != -1);

so in regards to your code above..

$scope.test= "HI How Are You ?";
alert($scope.test.indexOf("How") != -1); // return true;
ryeballar
  • 29,658
  • 10
  • 65
  • 74
0

Try the codes below:

ng-attr-target="{{(p.link.indexOf('https://')!=-1) ? '_blank' : _self}}"
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103