8

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 ?

2
  • Hi, check out the last part of my answer... it should solve your problems the way you expected Commented Aug 11, 2015 at 20:11
  • Possible duplicate of Initializing select with AngularJS and ng-repeat Commented Jan 11, 2016 at 14:18

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.