0

I am trying to find whether the provided element is present in array or not using indexOf function.

But, I am not able to get whether true or false based on condition.

I am given the fiddle url .

Kindly, I need some assistance. http://jsfiddle.net/vMzSq/17/

<div ng-controller="MyCtrl">
 Hello, {{name}}!<br>
 Super? {{contains(name)}}  
 </div>

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.name = 'Sai';
    $scope.searchNames = ['karthik','Sai']
    $scope.contains = function(searchString) {
        return searchString.indexOf(searchNames) != -1;
}

1 Answer 1

2

It needs to be $scope.searchNames, not just searchNames (it's undefined). Also, you need to do array.indexOf(element), not element.indexOf(array):

function MyCtrl($scope) {
  $scope.name = 'Sai';
  $scope.searchNames = ['karthik','Sai']
  $scope.contains = function(searchString) {
      return $scope.searchNames.indexOf(searchString) != -1;
      //  HERE ^^^
  }
}

http://jsfiddle.net/vMzSq/23/

Or, if you don't need it in the scope, you can remove it, but be consistent:

function MyCtrl($scope) {
  $scope.name = 'Sai';
  var searchNames = ['karthik','Sai']
  $scope.contains = function(searchString) {
      return searchNames.indexOf(searchString) != -1;
  }
}

http://jsfiddle.net/vMzSq/22/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.