簡體   English   中英

如何從按鈕單擊角度js的文本框中獲取值

[英]How to get the value from text box on button click angular js

  <form class="form-group has-success has-feedback" novalidate>
                <br/>
                <label class="control-label" for="inputSuccess2">Enter the Phone Number</label>
                <input type="number" class="form-control" ng-model="user.phonenum" name="phonenum" id="inputSuccess2">

                <button ng-click="myData.doClick(item, $event)" class="btn btn-primary">Get Mobile Phone Number Data</button>
            </form>

            <br/>
            Data from server: {{myData.fromServer.State}}


      <script>
        angular.module("myapp", [])
                .controller("MyController", function($scope, $http) {
                    $scope.myData = {};
                    $scope.myData.doClick = function(item, event) {

                        var responsePromise = $http.get("https://ajith-indian-mob-info.p.mashape.com/getInfo?mobno="+item, {headers: {'X-Mashape-Key': ''}});
                        responsePromise.success(function(data, status, headers, config) {
                            $scope.myData.fromServer = data;
                        });
                        responsePromise.error(function(data, status, headers, config) {
                            alert("AJAX failed!");
                        });
                    }


                });
    </script>

嗨,我是angular的新手,如何從文本框中獲取值到controller angular?

我在控制器中親自定義了對象用戶(為了更好地閱讀,這不是必需的)

$scope.user = {}

然后

<input type="number" class="form-control" ng-model="user.phonenum" name="phonenum" id="inputSuccess2">

會自動將此文本框的內容綁定到變量$scope.user.phonenum ,您可以輕松地訪問它:

$scope.myData.doClick = function() {
    var responsePromise = $http.get("https://ajith-indian-mob-info.p.mashape.com/getInfo?mobno="+$scope.user.phonenum, {headers: {'X-Mashape-Key': ''}});
    ...

您可以使用$scope.ModelName在控制器的范圍內獲取模型的值。

在任何需要電話號碼的位置執行此$scope.user.phonenum

但是,請檢查模型是否在控制器的范圍內。

使用Angular,您應該改變思維方式,在控制器中不使用視圖...使用模型(並讓指令更新該模型)。 因此,在您的示例中,文本框已映射到user.phonenum,因此您可以在控制器中執行類似$ scope.user.phonenum的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM