javascript - only execute function with ng-click if input fields are not empty -
i want perform function if other 2 fields not empty, not sure how it.
ng-click="savetolist($event)"
html
<div class="col-xs-5"> <label for="exampleinputpassword1">enter 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 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 id of input box - angular rocks! urllink.value = ''; //urlname id of input box - angular rocks! } }
i error when empty:
error: $scope.mvname undefined
the problem isn't angular: .trim()
doesn't work against undefined values. check see if variable defined before trying trim it, example:
var mvname = ($scope.mvname) ? $scope.mvname.trim() : '' ;
Comments
Post a Comment