0

i filled my dropdownlist by angular js from json. But selection is not working. How can i select dropdownlist item?

TaskTypeDefinitionController.js:

 var roleList = [{"id" :1, "value" :"test" },{"id" : 2, "value" : "test2"}];
        roleList.then(function (data) {
            $scope.roleList = data;
            var jsonArr = [];
            for (var i = 0; i < data.length; i++) {

                jsonArr.push({
                    id: data[i].roleName.serno,
                    value: data[i].roleName.value
                });
            }
            $scope.Names = jsonArr;
            console.log(jsonArr);
        });

html(View) :

<select class="form-control" placeholder="Görev Tipi Adı" ng-
options="field.id as field.value for field in Names" ng-model="Name"  
required></select>
3
  • you got selected value in your ng-model added console.log($scope.Name); and check in browser's console. Commented Aug 14, 2015 at 11:00
  • 1
    But selection is not working means default value is does not set? Commented Aug 14, 2015 at 11:02
  • Selected value will be present in the ngModel i.e. Name. Commented Aug 14, 2015 at 11:04

1 Answer 1

1

selection is not working means you need to set the default value of dropdown. If yes then try this below code instead of your code.

 var roleList = [{"id" :1, "value" :"test" },{"id" : 2, "value" : "test2"}];
        roleList.then(function (data) {
            $scope.roleList = data;
            var jsonArr = [];
            for (var i = 0; i < data.length; i++) {

                jsonArr.push({
                    id: data[i].roleName.serno,
                    value: data[i].roleName.value
                });
            }
             jsonArr.push({id:0,value:"Select Name"});
             $scope.Name=jsonArr[jsonArr.length-1];
            $scope.Names = jsonArr;
            console.log(jsonArr);
        });

Explain

The Name is your model name of dropdown. It is undefined on your first time. So I think it's may be showing blank. So you need to assign a value to the Name object. In my code have assign a value(Select Name) to the Name Object.

in this code

         jsonArr.push({id:0,value:"Select Name"});
         $scope.Name=jsonArr[jsonArr.length-1];
Sign up to request clarification or add additional context in comments.

2 Comments

connect with me for job offer : [email protected]. i am serious!
Seriously? ha ha !! [email protected]. Send mail to this address !

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.