0

Right now I have an input field:

<input type="number" ng-model="distance"></input>

In the JS I assign the distance to a value:

$scope.distance = 0;

I want this value to change when the user enters in a value and clicks a button. I have the button setup with my controller... But every time the button is clicked, it displays the value as 0.

Function when button is clicked:

//convert button function for when button is clicked
$scope.convert = function(myUnit, myUnit2, distance){
    alert(distance);
}

The button:

<button class="button button-block button-balanced" ng-click="convert(myUnit.thisunit, myUnit2.thisunit2, {{distance}})">
    Convert
</button>
2
  • added the function and button Commented Feb 25, 2015 at 21:40
  • Use convert(myUnit.thisunit, myUnit2.thisunit2, distance). You're already in an AngularJS expression, no need to add the braces {{…}}. Commented Feb 25, 2015 at 21:41

2 Answers 2

1

Change ng-click to this ng-click="convert(myUnit.thisunit, myUnit2.thisunit2, distance)" because you were using {{distance}} interpolation directive inside ng-click directive that will never pass distance value

HTML

<button class="button button-block button-balanced" ng-click="convert(myUnit.thisunit, myUnit2.thisunit2, distance)">
    Convert
</button>

Hope this could help you, Thanks.

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

Comments

0

Depending on the structure of your HTML, (if you have an ngIf above your <input>) the distance model may not be trickling up to your controller.

You should probably nest it for safety, like so:

$scope.formData = {distance: 0};

And then:

<input type="number" ng-model="formData.distance"></input>

Also, your click-handler should just read from the $scope value as well:

$scope.clickHandler = function() {
    alert(formData.distance);
}

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.