Get input value using angularjs

I am implementing angular js and trying to get the value of an input field and store it in local storage. The input is entered by the user, and he refers to the 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 the js code:

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

Why is this, using the above encoding, after I enter the ip address in the input field, $scope.serverip , I always get undefined?

+5
source share
3 answers

I will find out the correct answer. We have to pass serverip to 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); }; }) 
+1
source

Have you installed ng-app and ng-controller correctly? 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 }; }) 
0
source

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

Live: http://jsbin.com/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.com/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> 

0
source

All Articles