简体   繁体   中英

How to pass value of textbox to a method in controller?

In Angularjs, I'm trying to pass a value from a textbox to method written in controller as below

#Try 1

<input type="text" ng-blur="isExists(this.value)">

and within my controller I have

 $scope.isExists = function (theValue) {
    alert(theValue);
  };

It is not working.

#Try 2

<input type="text" ng-model="username" ng-blur="isExists()">

and within controller

$scope.isExists = function () {
        alert($scope.username); // returns undefined
      };

How to pass value from ng-blur to a method within a Controller?

Updates:

Any reason why the value is not seen in the textbox?

<input type="text" ng-model="username" ng-blur="isExists()">

Fiddle

Try2 should not return undefined if the <input type="text"

is filled with at least one character.

You can append {{ username }} on the html page just for debug purporses to make sure it was well binded.

<input type="text" name="userId" id="txtid" />

<script type="text/javascript">
    $(document).ready(function () {

        $('#txtid').blur(function () {
            debugger;
            var id = $('#txtid').val();

            window.location = "/Home/CreateSupplier/?userid=" + id;

        });
    });
</script>

In controller

 public ActionResult CreateSupplier(string userid)
        {
            if (userid != null)
            {
                return Content("Received Username:" + userid);
            }
            return View(thesupplierList);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM