AngularJS在范围内获取所选项目

Ale*_*lex 6 javascript angularjs ionic-framework

我正在使用Ionic和AngularJS开发应用程序.无法计算如何获得所选选项的值

调节器

.controller('TestCtrl', function($scope, $options) {
    $scope.options = [
        { id: 0, label: '15', value: 15 },
        { id: 1, label: '30', value: 30 },
        { id: 2, label: '60', value: 60 }
    ]

    $scope.countSelector = $scope.options[0];

    $scope.changeCount = function(obj){
        obj = JSON.parse(obj);
        console.log(obj)
        console.log(countSelector)
        $options.set('productCountPerPageValue', obj.value);
        $options.set('productCountPerPage', obj.id);
    };
    ...
})
Run Code Online (Sandbox Code Playgroud)

模板

<ion-list ng-controller="TestCtrl">

    <label class="item item-input item-select">
        <div class="input-label">
            {{countSelector}}
        </div>
        <select ng-model="countSelector" ng-change="changeCount('{{countSelector}}')" ng-options="opt as opt.label for opt in options">
        </select>
    </label>

</ion-list>
Run Code Online (Sandbox Code Playgroud)

console.log(obj)始终返回先前选择的值

console.log(countSelector)始终返回默认值(如果已设置)或未定义

Deb*_*ppe 5

当你这样做时select ng-model="countSelector",你将你的选择绑定到$scope.countSelector

因此,在控制器内部,如果要访问所选值,请使用以下命令:

$scope.countSelector
Run Code Online (Sandbox Code Playgroud)

编辑:

根据您的要求,您可能希望直接在$ scope.countSelector中包含值.为此,您可以将ng-options调整为以下内容:

ng-options="opt.id as opt.label for opt in options"
Run Code Online (Sandbox Code Playgroud)