简体   繁体   English

抓取对象ID并将其插入会话存储Angular

[英]Grab object id and insert into session storage Angular

I have a controller that populates a second drop-down box depending on the selection of the first, this works well although WebStorm sees it as a syntax error. 我有一个控制器,该控制器根据第一个下拉框的填充来填充第二个下拉框,尽管WebStorm将其视为语法错误,但此方法运行良好。 I know want to post the ID of the format fname to my session storage to use else where, however I have tried the code bellow and I see Value as 我知道想要将fname格式的ID发布到我的会话存储中以在其他地方使用,但是我尝试了下面的代码,我看到Value为

[object Object] [对象对象]

if I use 如果我用

sessionStorage.setItem('format', $scope.formats);

and

undefined 未定义

if I use 如果我用

sessionStorage.setItem('format', $scope.formats.id); sessionStorage.setItem('format',$ scope.formats.id);

I have also tried 我也尝试过

sessionStorage.setItem('format', $scope.formats.formatType.id);

I get nothing not even a empty value 我什么也没有得到,即使是空值

how do I correctly add the ID to my format value in my session storage ? 如何正确将ID添加到会话存储中的格式值中?

Full code 完整代码

FirstModule.controller('dropDown', function ($scope) {

$scope.productsandformats = [
    {
        "name": "staff",
        "format": [
            {"Fname": "bill", "id": "1"},
            {"Fname": "bob", "id": "2"},
            {"Fname": "tom", "id": "3"}
        ]
    },
    {
        "name": "managers",
        "format": [
            {"Fname": "dick", "id": "4"},
            {"Fname": "jhon", "id": "5"}
        ]
    },

    {
        "name": "directors",
        "format": [
            {"Fname": "willy", "id": "6"}
        ]
    },


    {
        "name": "temp",
        "format": [
            {"Fname": "jeff", "id": "18"},
            {"Fname": "billy", "id": "19"}
        ]
    }];

$scope.productTypeChange = function () {
    $scope.formats = $scope.productsandformats.find(ps => ps.name == $scope.formData.ProductType.name)

    sessionStorage.setItem('format', $scope.formats.id);

}

}); });

To further more explain how I get the ID, The code bellow enables the user to select A fname from the name type for example if select staff you get bill, bob and tom then after you select one it,ll print out the selected name and fname including the fname id, the fname id is what i need to send to the session storage 为了进一步说明我如何获取ID,下面的代码使用户可以从名称类型中选择一个fname,例如,如果选择职员,您将获得帐单,鲍勃和汤姆,然后在选择它之后,将打印出选定的名称并fname包括fname id,fname id是我需要发送到会话存储的内容

HTML 的HTML

<div class="radio col-sm-6" style="margin-top: 0 !important;">
    <label>
        <input type="radio" ng-model="formData.type" value="Create" >
        <span class="glyphicon glyphicon-unchecked">
        </span>
        Need to make a poster?
    </label>
</div>

<div class="radio col-sm-6">
    <label>
        <input type="radio" ng-model="formData.type" value="Preview">
        <span class="glyphicon glyphicon-eye-open">
        </span>
        Already have a poster?
    </label>
</div>

You cannot store an object , you can only store a string . 您不能存储object ,只能存储string

To store an object you need to "stringify" it. 要存储一个对象,您需要对其进行“字符串化”。

var jsonItem = JSON.stringify($scope.formats);
sessionStorage.setItem('format', jsonItem);

Then to retrieve it you need to parse it 然后要检索它,您需要解析它

var item = sessionStorage.getItem('format');
item = JSON.parse(item)

From MDN Storage/setItem 从MDN Storage / setItem

storage.setItem(keyName, keyValue);

keyName A DOMString containing the name of the key you want to create/update. keyName一个DOMString,其中包含要创建/更新的密钥的名称。

keyValue A DOMString containing the value you want to give the key you are creating/updating. keyValue一个DOMString,包含要提供要创建/更新的键的值。

About finding the ID 关于查找ID

When you do 当你做

$scope.formats = $scope.productsandformats.find(ps => ps.name == 
 $scope.formData.ProductType.name)

You get an object with this structure 您将获得具有此结构的对象

{
    "name": "staff",
    "format": [
        {"Fname": "bill", "id": "1"},
        {"Fname": "bob", "id": "2"},
        {"Fname": "tom", "id": "3"}
    ]
},

So i guess the id is contained inside item.format[0].id for the first item and so on for the subsequent items. 所以我想id包含在item.format[0].id用于第一个项目,以此类推。

format is an array so you wont be able to directly access id if you don't first select which element of the array you want (eg. format[0] ). format是一个数组,因此,如果您不先选择所需的数组元素(例如format[0] ),就无法直接访问id

So I used the answer above but to achieve the id I had to use this 所以我使用了上面的答案,但是要获得ID我必须使用它

 $scope.myFunc = function() {
        var jsonItem = JSON.parse($scope.formData.formatType.id);
        sessionStorage.setItem('format', jsonItem);
    }

@Bolza answer was good but I needed more so kept digging and experimenting @Bolza的回答很好,但我需要更多,所以请继续进行挖掘和试验

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

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