Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input value using angularjs

I'm implementing angular js and is trying to get the input box's value and store it into the local storage. The input is typed by user and it's refer to ip address.

Below is my html code:

  <div>
    <input ng-model="serverip">
    <input type="button" class="button" value="Apply" ng-click="save()">
  </div>

Below is my js code:

.controller('Ctrl', function($scope) {

    $scope.save= function() {
        console.log($scope.serverip);
        localStorage.setItem('serverip', $scope.serverip);
    };
})

Why is it by using the above coding, after I key in the ip address into the input box, the $scope.serverip I get is always undefine?

like image 216
Coolguy Avatar asked Jun 03 '15 05:06

Coolguy


1 Answers

I kinda find out the correct answer. We have to pass back the serverip in the html:

  <div>
    <input ng-model="serverip">
    <input type="button" class="button" value="Apply" ng-click="save(serverip)">
  </div>

And in the js file:

.controller('Ctrl', function($scope) {

    $scope.save = function(serverip) {
        console.log(serverip);
        localStorage.setItem('serverip', serverip);
    };
})
like image 172
Coolguy Avatar answered Oct 16 '22 00:10

Coolguy