admin管理员组文章数量:1432343
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?
- 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
3 Answers
Reset to default 1I 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
版权声明:本文标题:javascript - Get input value using angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745603934a2665728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论