0

I only want to perform this function if the other two fields are not empty, not sure how to do it.

ng-click="saveToList($event)" 

HTML

  <div class="col-xs-5">
              <label for="exampleInputPassword1">Enter a Title/Description</label>
                <input type="text" id="urlName" class="form-control" placeholder=""  ng-model="mvName" />
            </div>
            <div class="col-xs-5">
                <label for="exampleInputPassword1">Enter a URL</label>
                <input type="text" id="urlLink" class="form-control" placeholder=""  ng-model="mvUrl" />
          </div>
          <div class="col-xs-2">
              <a href="javascript:" ng-click="saveToList($event)" class="btn btn-block post">Post</a>
          </div>

app.js

$scope.saveToList = function(event) {

    var mvName = $scope.mvName.trim();
    var mvUrl = $scope.mvUrl.trim();

    if (mvName.length > 0) {
      $scope.favUrls.$add({
        name: mvName,
        title: mvUrl
      });
        urlName.value = ''; //urlName is the ID of  input box - Angular rocks!
      urlLink.value = ''; //urlName is the ID of  input box - Angular rocks!
    }

}

I get this error when they are empty:

Error: $scope.mvName is undefined

6 Answers 6

1

The problem isn't anything to do with Angular: .trim() doesn't work against undefined values. Check to see if the variable is defined before trying to trim it, for example:

var mvName = ($scope.mvName) ? $scope.mvName.trim() : '' ;
Sign up to request clarification or add additional context in comments.

Comments

1

You should initialize $scope.mvName in your controller! As it is empty, you are getting an error at line var mvName = $scope.mvName.trim();

EDIT

<a href="javascript:" ng-click="(!mvName && !mvUrl) ? saveToList($event) : ''" class="btn btn-block post">Post</a>

1 Comment

whoever given downvote probably don't know any angularJS OR didn't understand the question! :(
0

You can add ng-disabled directive to your a tag. Something like this:

<a href ng-disabled="!mvName || !mvUrl" ng-click="saveToList($event)" class="btn btn-block post">Post</a>

or check that vars in your js:

$scope.saveToList = function(event) {
   if(!$scope.mvName || !$scope.mvUrl)
      return;

    var mvName = $scope.mvName.trim();
    var mvUrl = $scope.mvUrl.trim();
...

Comments

0

You could do it like this:

<div class="col-xs-5">
  <label for="exampleInputPassword1">Enter a Title/Description</label>
    <input type="text" id="urlName" class="form-control" placeholder=""  ng-model="mvName" />
</div>
<div class="col-xs-5">
    <label for="exampleInputPassword1">Enter a URL</label>
    <input type="text" id="urlLink" class="form-control" placeholder=""  ng-model="mvUrl" />
</div>
<div class="col-xs-2">
  <a href="javascript:" 
    ng-click="(mvName != '' && mvUrl != '') && saveToList($event)" class="btn btn-block post">Post</a>
</div>

Or use ng-disabled="mvName === '' && mvUrl === ''"

Other options would be to not render the button: ng-if or ng-show.

2 Comments

Why not to do ng-click="mvName && mvUrl && saveToList($event)" instead of ng-click="(mvName != '' && mvUrl != '') && saveToList($event)"?
Also possible I suppose. I prefer more explicit code/checks in javascript.
0

How about performing a simple check to see if the fields are empty or not, before you perform any comparisons using the value. That should solve the issue I believe.

Comments

0
    <form name="myForm">
      <div class="col-xs-5">
        <label for="exampleInputPassword1">Enter a Title/Description</label>
        <input type="text" id="urlName" required class="form-control" placeholder=""  ng-model="mvName" />
      </div>
      <div class="col-xs-5">
        <label for="exampleInputPassword1">Enter a URL</label>
        <input type="text" id="urlLink" required class="form-control" placeholder=""  ng-model="mvUrl" />
      </div>
      <div class="col-xs-2">
        <a href="javascript:" ng-enabled="myForm.$valid" ng-click="saveToList($event)" class="btn btn-block post">Post</a>
      </div>    
    </form>

Try using the angular validation.

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.