Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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
如何设置a的初始值<;选择>;和AngularJS?_Angularjs - Fatal编程技术网

如何设置a的初始值<;选择>;和AngularJS?

如何设置a的初始值<;选择>;和AngularJS?,angularjs,Angularjs,我正在控制器中使用以下代码: $scope.$watch('option.selectedPageType', function () { if ($scope.isNumber($scope.option.selectedPageType)) { localStorageService.add('selectedPageType', $scope.option.selectedPageType); } }) getPageTypes: function ($s

我正在控制器中使用以下代码:

$scope.$watch('option.selectedPageType', function () {
    if ($scope.isNumber($scope.option.selectedPageType)) {
        localStorageService.add('selectedPageType', $scope.option.selectedPageType);
    }
})

getPageTypes: function ($scope) {
            $scope.option.pageTypes = [
                { id: 0, type: 'Edit Basic' },
                { id: 1, type: 'Edit Standard' },
                { id: 2, type: 'Report' }
            ];
            $scope.option.selectedPageType = parseInt(localStorageService.get('selectedPageType'));
},
/* assume want first item in array */
$scope.option.selectedPageType = localStorageService.get('selectedPageType') || $scope.option.pageTypes[0].id
在我的HTML中:

<select data-ng-model="option.selectedPageType"
        data-ng-options="item.id as item.type for item in option.pageTypes">
        <option style="display: none" value="">Select Page Type</option>
</select>

选择页面类型

而不是使用“选择页面类型”选项。如何使我的代码默认为本地存储中的值,或者如果没有,则默认为option.pageTypes列表中的一个值?

尝试使用
上的
ng init



查看此小提琴:

如果未存储任何内容,请让您的
localStorageService
返回null。如果它确实存在,请让服务返回整数

然后在控制器中:

$scope.$watch('option.selectedPageType', function () {
    if ($scope.isNumber($scope.option.selectedPageType)) {
        localStorageService.add('selectedPageType', $scope.option.selectedPageType);
    }
})

getPageTypes: function ($scope) {
            $scope.option.pageTypes = [
                { id: 0, type: 'Edit Basic' },
                { id: 1, type: 'Edit Standard' },
                { id: 2, type: 'Report' }
            ];
            $scope.option.selectedPageType = parseInt(localStorageService.get('selectedPageType'));
},
/* assume want first item in array */
$scope.option.selectedPageType = localStorageService.get('selectedPageType') || $scope.option.pageTypes[0].id