Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs select without ng-options or static options

How, to use a static select in angularjs.

I wanna create a select ng-model with change event with statc values.

<form ng-controller="mycontroller">
    <select class="form-control"  name='modelid' ng-model="modelid"  ng-change="modelidchange">
        <option value="-1">select </option>       
        <option value='4'>Static text 4</option>
        <option value='1'>Static text 1</option>
        <option value='2'>Static text 2</option>                 
    </select>
</form>

my controller:

angular.module('app').controller('chapter',function($scope,$http) {
    console.log('ok')
    $scope.id = modelid
    alert($scope.id)
});

I just wanna get de model value, but this error:

Error: ngOptions:iexp
Invalid Expression

I DON'T WANNA USE NG-OPTIONS just a static select.

Can somebody help me ?

like image 627
Adilson Santos da Rocha Avatar asked Aug 11 '15 19:08

Adilson Santos da Rocha


Video Answer


2 Answers

check this jsfiddle link, maybe it will help

<select ng-model="filterCondition.operator">
    <option ng-selected="{{operator.value == filterCondition.operator}}" ng-repeat="operator in operators" value="{{operator.value}}">{{operator.displayName}}</option>

and this answer

like image 197
macrog Avatar answered Oct 22 '22 21:10

macrog


You first need to change your AngularJS Code a little bit...

From this

angular.module('app').controller('chapter',function($scope,$http){
                   console.log('ok')
    $scope.id = modelid
    alert($scope.id)
});

To this

angular.module('app').controller('chapter',function($scope,$http){
    $scope.id = $scope.modelid;
    alert($scope.id);
});

Next your Controller doesn't match... In HTML your Controller is

<form ng-controller="mycontroller">

In Angular your Controller is

.controller('chapter'...

So change it from this

<form ng-controller="mycontroller">

to this

<form ng-controller="chapter">

And last but not least :-)

You need to define the function you want to call with ng-change in your controller... So change the whole code to the following...

HTML

<form ng-controller="chapter">
    <select class="form-control"  name='modelid' ng-model="modelid"  ng-change="modelidchange()">
    <option value="-1">select </option>       
    <option value='4'>Static text 4</option>
    <option value='1'>Static text 1</option>
    <option value='2'>Static text 2</option>
</select>

Angular

angular.module('app').controller('chapter',function($scope,$http){
    $scope.modelidchange = function () {
        $scope.id = $scope.modelid;
        alert($scope.id)
    }
});
like image 29
Dennis Weidmann Avatar answered Oct 22 '22 22:10

Dennis Weidmann