javascript - Set ng-model value to button click - Stack Overflow

I have this little snippet of code to set the root.lowWeight and root.highWeight to my controller.<i

I have this little snippet of code to set the root.lowWeight and root.highWeight to my controller.

<input type="number" placeholder="Enter Low Range..." class="form-control input-xs">
<input type="number" placeholder="Enter High Range..." class="form-control input-xs">
<button class="btn btn-primary" ng-click="assignWeights()">Select Model</button>

Controller model:

//set the initial root scope to empty object
$scope.root = {};
//define root weight
$scope.root.lowWeight = 0;
$scope.root.highWeight = 0;

I know I could just use ng-model="root.lowWeight" to set the value, but ng-model triggers on element input event.

How can I either:

a) Send the input values in via assignWeights(param1, param2)

b) Change ng-model to trigger input on button click (seems hacky, less preferred solution)

I also know I could use JS/jQuery to trigger the input event and block the input event, but that's a last ditch effort I will 99.9% not do.

I have this little snippet of code to set the root.lowWeight and root.highWeight to my controller.

<input type="number" placeholder="Enter Low Range..." class="form-control input-xs">
<input type="number" placeholder="Enter High Range..." class="form-control input-xs">
<button class="btn btn-primary" ng-click="assignWeights()">Select Model</button>

Controller model:

//set the initial root scope to empty object
$scope.root = {};
//define root weight
$scope.root.lowWeight = 0;
$scope.root.highWeight = 0;

I know I could just use ng-model="root.lowWeight" to set the value, but ng-model triggers on element input event.

How can I either:

a) Send the input values in via assignWeights(param1, param2)

b) Change ng-model to trigger input on button click (seems hacky, less preferred solution)

I also know I could use JS/jQuery to trigger the input event and block the input event, but that's a last ditch effort I will 99.9% not do.

Share Improve this question edited Nov 23, 2015 at 16:32 yAnTar 4,62010 gold badges51 silver badges74 bronze badges asked Nov 23, 2015 at 16:20 Sterling ArcherSterling Archer 22.4k19 gold badges85 silver badges121 bronze badges 7
  • not sure I understand correctly, but maybe you seek this: docs.angularjs/api/ng/directive/ngModelOptions – Nitsan Baleli Commented Nov 23, 2015 at 16:28
  • I am not sure what you are trying to do, can't you just use ng-model, then the function assigns values to the model values? – trees_are_great Commented Nov 23, 2015 at 16:29
  • @NitsanBaleli that's close, I could do ng-model-options="{ updateOn: 'blur' }" and the button click will "act" like a click event, but I'd like to have it purely assigned to the button. – Sterling Archer Commented Nov 23, 2015 at 16:31
  • @Sam as I said, the ng-model is bound to the input event, so while it would work, it would not be bound to the button click. I need to have it assigned to the model when the button is clicked – Sterling Archer Commented Nov 23, 2015 at 16:32
  • 2 one solution would be to assign it to the model under a different name - inputData.lowWeight, then function $scope.root.lowWeight = $scope.inputData.lowWeight on button click – trees_are_great Commented Nov 23, 2015 at 16:34
 |  Show 2 more ments

2 Answers 2

Reset to default 3

Solution using seperate scope object: plunkr

Html:

Low: <input type="text" name="lowRange" ng-model="inputData.lowWeight">
High: <input type="text" name="highRange" ng-model="inputData.highWeight">

Js:

 $scope.assignWeights = function() {
        $scope.lowWeight = $scope.inputData.lowWeight;
        $scope.highWeight = $scope.inputData.highWeight
  }

In your assignWeights function, you could use a selector to retrieve the values of the inputs and then assign your scope variable values based on that. Just assign a name to each input.

E.g.

<input type="number" placeholder="Enter Low Range..." class="form-control input-xs" name="lowRange">
<input type="number" placeholder="Enter High Range..." class="form-control input-xs" name="highRange">
<button class="btn btn-primary" ng-click="assignWeights()">Select Model</button>

Controller:

$scope.assignWeights = function() {
    $scope.root.lowWeight = document.querySelector('input[name="lowRange"]').value
    $scope.root.highWeight = document.querySelect('input[name="highRange"]').value
}

Plunker: http://plnkr.co/edit/lm8GFA0CD0zWwgAMSLah?p=preview

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745199911a4616274.html

相关推荐

  • javascript - Set ng-model value to button click - Stack Overflow

    I have this little snippet of code to set the root.lowWeight and root.highWeight to my controller.<i

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信