admin管理员组

文章数量:1023758

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?

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?

Share Improve this question asked Jun 3, 2015 at 5:41 CoolguyCoolguy 2,28512 gold badges56 silver badges81 bronze badges 3
  • 1 How do you bind your controller to the HTML? – Mehrdad Kamelzadeh Commented Jun 3, 2015 at 5:47
  • I'm using the ionic platform – Coolguy Commented Jun 3, 2015 at 6:13
  • can you provide plunker or jsfiddle with problem? – Grundy Commented Jun 3, 2015 at 6:47
Add a ment  | 

3 Answers 3

Reset to default 1

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);
    };
})

Have you properly set ng-app and ng-controller? Here's a plunker demonstrating the behavior you want.

HTML:

<body ng-controller="Ctrl">
  <div>
    <input ng-model="serverip">
    <input type="button" class="button" value="Apply" ng-click="save()">
  </div>
  <p>
    Saved IP: {{savedip}}
  </p>
</body>

Controller:

var app = angular.module('plunker', []);

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

    $scope.save = function() {
        $scope.savedip = $scope.serverip
    };
})

This is the perfect working example of what you are looking:

Live: http://jsbin./bifiseyese/1/edit?html,console,output

Code:

<div ng-app="app" ng-controller="Ctrl">
    <input type="text" ng-model="serverip"/>
    <button ng-click="save()">Save</button>
</div>

<script src="//cdnjs.cloudflare./ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<script type="text/javascript">
angular.module('app',[]).controller('Ctrl', ['$scope', function($scope){
    $scope.save = function(){
        localStorage.setItem('serverip', $scope.serverip);
        console.log("localStorage.serverip = " + localStorage.serverip);
    };
}]);
</script>

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?

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?

Share Improve this question asked Jun 3, 2015 at 5:41 CoolguyCoolguy 2,28512 gold badges56 silver badges81 bronze badges 3
  • 1 How do you bind your controller to the HTML? – Mehrdad Kamelzadeh Commented Jun 3, 2015 at 5:47
  • I'm using the ionic platform – Coolguy Commented Jun 3, 2015 at 6:13
  • can you provide plunker or jsfiddle with problem? – Grundy Commented Jun 3, 2015 at 6:47
Add a ment  | 

3 Answers 3

Reset to default 1

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);
    };
})

Have you properly set ng-app and ng-controller? Here's a plunker demonstrating the behavior you want.

HTML:

<body ng-controller="Ctrl">
  <div>
    <input ng-model="serverip">
    <input type="button" class="button" value="Apply" ng-click="save()">
  </div>
  <p>
    Saved IP: {{savedip}}
  </p>
</body>

Controller:

var app = angular.module('plunker', []);

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

    $scope.save = function() {
        $scope.savedip = $scope.serverip
    };
})

This is the perfect working example of what you are looking:

Live: http://jsbin./bifiseyese/1/edit?html,console,output

Code:

<div ng-app="app" ng-controller="Ctrl">
    <input type="text" ng-model="serverip"/>
    <button ng-click="save()">Save</button>
</div>

<script src="//cdnjs.cloudflare./ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<script type="text/javascript">
angular.module('app',[]).controller('Ctrl', ['$scope', function($scope){
    $scope.save = function(){
        localStorage.setItem('serverip', $scope.serverip);
        console.log("localStorage.serverip = " + localStorage.serverip);
    };
}]);
</script>

本文标签: javascriptGet input value using angularjsStack Overflow