1

I have an AngularJS function that uses JavaScript's RegExp() to look for a character at the start of a string. In this case, it's a lowercase p.

    $scope.checkState = function(state){
      var page = state;
      console.log(page);
      var regex = new RegExp("^[p]");
      var check = regex.exec(page);
      console.log('Regex evaluated to: ' + check);
      if (check == "p")
        return false;
      else
        return true;
    }

On the main HTML page, there is a button that is checking for an answer to the status of checkState(). If it returns as false, it hides the button from view with ng-show.

<md-button class="md-raised md-warn btn-right" ng-show="checkState({{pageID}})" ng-model="thing" ng-cloak onclick="thing()">My Button</md-button>

In here, pageID is a variable that contains a given ID value of a page template. When called in Angular, pageID becomes a string that is passed to the checkState() function.

Right now, I have been able to get the script to evaluate to null, but not to any other results. This is because despite the variable being filled in programatically, I get undefined instead.

What's going on here?

2
  • 2
    but you can simply do ng-show="checkState(pageID)" and that should work Commented Mar 28, 2016 at 16:12
  • 1
    Was just about to say, most any Angular directive will track these changes with each digest cycle. I don't know if that's the core problem, but it could be. Commented Mar 28, 2016 at 16:13

1 Answer 1

2

Many thanks to @entre and @Harris Weinstein for their help. The problem was here:

ng-show="checkState({{pageID}})"

I had incorrectly thought that I needed to use the curly braces in order to get the string I needed. However, Angular doesn't need that when using its own function calls. Therefore, the correct way to pass the string is:

ng-show="checkState(pageID)"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.