2

AngularJS receives a selected element in its scope

 3 years ago
source link: https://www.codesd.com/item/angularjs-receives-a-selected-element-in-its-scope.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AngularJS receives a selected element in its scope

advertisements

I'm developing app with Ionic and AngularJS. Can't figure how to get value of selected option

Controller

.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);
    };
    ...
})

Template

<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>

console.log(obj) Always return previously selected value

console.log(countSelector) Always return default value (if set) or undefined


When you do select ng-model="countSelector", you are binding your select to $scope.countSelector

So, inside your controller, if you want to have an access to your selected value, you use the following :

$scope.countSelector

edit :

according to your requirements, you might want to have directly the value inside $scope.countSelector. To do so, you can adapt your ng-options to the following :

ng-options="opt.id as opt.label for opt in options"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK