Naveenam K
Naveenam K

Reputation: 37

AngularJS: Function call in ternary operator conditional check

My HAML file:

%pipes-autocomplete{:model =>"filter.value",:option => "validate_option(filter)" ? "dependant(filter)" : "filter.option"}

My Coffee Script:

  $scope.validate_option =(filter)->
     console.log "called validate_option"
     if filter.hasOwnProperty('option') && filter.option.indexOf('dependant') > -1
       return true
     else
      return false
  $scope.dependant =(cal)->
    return "choosed"

In the ternary operator I'm trying to call the validate_option function defined in my angular controller.But the function is not getting called.Can someone help me with this problem.

Upvotes: 0

Views: 1095

Answers (1)

trincot
trincot

Reputation: 350077

As you put it in the question, the ternary operator will consider the string "validate_option(filter)" to be true (just because it is not an empty one).

Instead put the ternary operator inside the string:

"validate_option(filter) ? dependant(filter) : filter.option"

That way you defer the execution of the ternary operator to when the string is actually evaluated.

Upvotes: 2

Related Questions