admin管理员组文章数量:1396739
how to get a value from select (code) and send to controller in angular
for is using in the this controller??
My select consumes a web service
code:
<div class="form-group col-xs-12 offset-xs-2 col-md-3" ng-controller="productCtrl">
<label for="selectProduct">Product</label> <br>
<select class="form-control" id="product" required data-error="Select a Product!" tabindex="1">
<option value="" selected >Select a Product!</option>
<option value="{{product.codigo}}" ng-repeat="product in products">{{product.name}}</option>
</select>
<div class="help-block with-errors"></div>
</div>
how to get a value from select (code) and send to controller in angular
for is using in the this controller??
My select consumes a web service
code:
<div class="form-group col-xs-12 offset-xs-2 col-md-3" ng-controller="productCtrl">
<label for="selectProduct">Product</label> <br>
<select class="form-control" id="product" required data-error="Select a Product!" tabindex="1">
<option value="" selected >Select a Product!</option>
<option value="{{product.codigo}}" ng-repeat="product in products">{{product.name}}</option>
</select>
<div class="help-block with-errors"></div>
</div>
Share
Improve this question
asked Nov 1, 2016 at 17:12
IgorDuarteIgorDuarte
932 silver badges6 bronze badges
2
- 2 The concept with angular is that view should be controlled by the controller. Therefore you shouldn't really send the value to controller but let the controller control the view. Take a look at ngOption documentation and use ngModel to bind the value – Endless Commented Nov 1, 2016 at 17:18
-
Agree with the above -- definitely check out the
ngOption
documentation – Nathan Beck Commented Nov 1, 2016 at 17:43
2 Answers
Reset to default 4You have to use ng-model for this. Just change your select like this
<select class="form-control" id="product" ng-model="selectedProduct" required data-error="Select a Product!" tabindex="1">
<option value="" selected >Select a Product!</option>
<option value="{{product.codigo}}" ng-repeat="product in products">{{product.name}}</option>
</select>
Then in your controller you can access its value using
$scope.selectedProduct
For mor info about how the NgModel works you should read the docs
Use ngModel. You may also want to use ngOptions to generate the option tags for you.
For example:
<select ng-model="value" ng-options="option.value as option.label for option in options"></select>
The value will be available on your controller's scope ($scope.value
). $scope.options
should be an array of objects defining your options.
本文标签: javascriptAngularJS how to get selected option value and send to controllerStack Overflow
版权声明:本文标题:javascript - AngularJS: how to get selected option value and send to controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744118730a2591634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论