Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使AngularJS中的下拉列表默认为localstorage中的值?_Angularjs - Fatal编程技术网

如何使AngularJS中的下拉列表默认为localstorage中的值?

如何使AngularJS中的下拉列表默认为localstorage中的值?,angularjs,Angularjs,我有一个例子,我正在填充一个值列表。一切正常,我看到了预期的列表。但是现在我希望将该值设置为本地存储的值(如果可用) 我有下面的代码。当我运行此代码时,标签中类型后面的数字会更改为与localstorage中的数字匹配,但选择框不会更改 getContentTypeSelect: function ($scope) { entityService.getEntities('ContentType') .then(function (result) {

我有一个例子,我正在填充一个值列表。一切正常,我看到了预期的列表。但是现在我希望将该值设置为本地存储的值(如果可用)

我有下面的代码。当我运行此代码时,标签中类型后面的数字会更改为与localstorage中的数字匹配,但选择框不会更改

getContentTypeSelect: function ($scope) {
    entityService.getEntities('ContentType')
        .then(function (result) {
            $scope.option.contentTypes = result;
            $scope.option.contentTypesPlus = [{ id: 0, name: '*' }].concat(result);
            $scope.option.selectedContentType 
                 = localStorageService.get('selectedContentType');
        }, function (result) {
            alert("Error: No data returned");
        });
},

<span class="label">Type {{ option.selectedContentType }}</span>
   <select
       data-ng-disabled="!option.selectedSubject"
       data-ng-model="option.selectedContentType"
       data-ng-options="item.id as item.name for item in option.contentTypesPlus">
       <option style="display: none" value="">Select Content Type</option>
   </select>
以下是存储在contentTypesPlus中的数据:

0: Object
id: 0
name: "*"
__proto__: Object
1: b
id: 1
name: "Page"
__proto__: b
2: b
id: 2
name: "Menu"
__proto__: b
3: b
id: 3
name: "Content Block"
__proto__: b
如果存在localstorage值,如何使选择框变为localstorage值

更新


仍然希望得到一个答案,如果有人能帮助我,我会很乐意接受另一个答案。给出的答案不完整,我仍在等待更多信息。我希望有人能给我一个适合我的代码的例子。谢谢

我认为您需要根据select指令中定义的理解表达式,确保selectedContentType必须是option对象的id字段

综合表达式定义为

item.id as item.name for item in option.contentTypesPlus

item.id将是存储在ng型号选项中的实际值。selectedContentType

表示这是您选择的ng型号

<select 
    ng-model="language.selected" 
    ng-init="language.selected = settings.language[settings.language.selected.id]" 
    ng-options="l.label for l in settings.language" 
    ng-change="updateLanguage(language.selected)">

我想您需要将selectedContentType设置为范围变量$scope.option.selectedContentType=localStorageService.get'selectedContentType';很抱歉那是个打字错误。我在代码中正确地设置了它,但在这里没有设置。还是不起作用,当设置像你提到的。是的,这是一个打字错误。谢谢你指出这一点。您能解释一下selectedContentType需要作为选项对象的id字段是什么意思吗。如果你能给我举个很好的例子。对不起,我不明白。谢谢谢谢,但是我的代码怎么能做到这一点呢?例如,我有佩奇。我根本不知道你的意思是什么,也不知道如何修改我的代码以使其符合你所说的。sza意味着,如果本地存储的值与contentTypesPlus数组的id匹配,angular将把它设置为默认值,请参见。您的本地存储返回了什么值?谢谢您的建议。我用用于设置本地存储的代码更新了这个问题。其中的值为0,1,2。。3等。这些确实与contentTypesPlus数组中的值匹配,但仍然无法正确更改值。我认为问题在于,从localStorage中输出的值是以字符串而不是整数返回的。尝试$scope.option.selectedContentType=parseIntlocalStorageService.get'selectedContentType';
<select 
    ng-model="language.selected" 
    ng-init="language.selected = settings.language[settings.language.selected.id]" 
    ng-options="l.label for l in settings.language" 
    ng-change="updateLanguage(language.selected)">
$scope.settings = {
enableEventsNotification: true,
enableiBeaconNotification: true,
hours: [
    { id: 0, label: '3h', value: 3 },
    { id: 1, label: '6h', value: 6 },
    { id: 2, label: '9h', value: 9 },
    { id: 3, label: '12h', value: 12 },
    { id: 4, label: '24h', value: 24 }
  ],
language: [
    { id: 0, label: 'English', value: 'en-us' },
    { id: 1, label: 'Francais', value: 'fr-fr' }
  ] 
};
$scope.hours = [];
$scope.hours.selected = $localstorage.getObject('hours',   $scope.settings.hours[1]);
$scope.settings.hours.selected = $localstorage.getObject('hours', $scope.settings.hours[1]);
$scope.hours.selected = $scope.hours.selected;