简体   繁体   English

使用angularjs从控制器将本地存储值放在文本框中

[英]place localstorage value in a textbox from a controller with angularjs

I have a localstorage value which i would want to put in it a texfield from a controller 我有一个localstorage值,我想从控制器将其放入texfield

.controller('working_ctrl', ['$scope', '$http', '$location', function ($scope, $http,$location) {
     $scope.user= localStorage.getItem("user")
     $scope.items = [];
    $scope.selected = undefined;
    $http({
        method: 'GET',
        url:'http://localhost/work/templates/spree/work/items.php'
    }).then(function (data) {
        $scope.items = data.data;
        //console.log(JSON.stringify(data));

  })        
}])

HTML 的HTML

<div ng-controller="working_ctrl" ng-repeat="s in items| limitTo:1">
<input name="quantity" type="text" id="quantity" ng-model="s.quantity">
<input name="user" type="text" id="user" ng-model="s.user">
</div>

您可以将$scope.user绑定到该文本框

<input name="user" type="text" id="user" ng-model="user">

your try to put the user in User input field but your input field is present with in ng-repeat so push user data in Array or set the user data in specific array index . 您尝试将用户放在User输入字段中,但是ng-repeat中存在您的输入字段,因此将用户数据推送到Array或将用户数据设置在特定的数组索引中。 like this 像这样

    .controller('working_ctrl', ['$scope', '$http', '$location', function($scope, $http, $location) {
    $scope.user = localStorage.getItem("user")
    $scope.items = [];
    $scope.selected = undefined;
    $http({
        method: 'GET',
        url: 'http://localhost/work/templates/spree/work/items.php'
    }).then(function(data) {
        $scope.items = data.data;
        //console.log(JSON.stringify(data));

        $scope.item.forEach(function(value) {
            value.user = $scope.user
        })
        or // 

        $scope.item[0].user = $scope.user // user specific index
    })
}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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