user6656724
user6656724

Reputation:

angularjs unable to call function dynamically

i am creating a web app in which i have two dropdownlist

<tr style="width:80%; border:1px solid black;">
              <td style="text-align:center; width:50%;">
                  Comname
              </td>
              <td style="width:50%; border:1px solid black; padding:3px; text-align:center;">
                  <select ng-change="gbrandname()" ng-init="ucomname='comname'" ng-model="ucomname">
                      <option ng-repeat="o in comnamelistfun" value="comname">{{o.comname}}</option>
                  </select>
              </td>
          </tr>
          <tr style="width:80%; border:1px solid black;">
              <td style="text-align:center; width:50%;">
                  Brand Name
              </td>
              <td style="width:50%; border:1px solid black; padding:3px; text-align:center;">
                  <select ng-change="getzone()" ng-init="ubrandname='select'" ng-model="ubrandname">
                      <option ng-repeat="o in gbrand" value="brandname">{{o.brandname}}</option>
                  </select>
              </td>
          </tr>

on comname change i want to call the data from my database as per the selected company of my first dropdownlist

here is my function

//brandname
        $scope.gbrandname = function () {
            $http.get('/csuv5.asmx/getbrand', {
                params: {
                    log: 'admin',
                    comname: 'QED Productions Pvt Ltd',
                    pm: 'admin'
                }
            })

            .then(function (response) {
                {
                    $scope.gbrand = response.data.brandname;
                    console.log(response.data.branname);
                }
            });
    }

i pass those parameters in my function and print it but it is not showing me any data on my second dropdownlist

but when i pass static parameters in my function its working

this is my function with static parameter, this is working fine

//brandname
        //$scope.gbrandname = function () {
            $http.get('/csuv5.asmx/getbrand', {
                params: {
                    log: 'admin',
                    comname: 'QED Productions Pvt Ltd',
                    pm: 'admin'
                }
            })

            .then(function (response) {
                {
                    $scope.gbrand = response.data.brandname;
                    console.log(response.data.branname);
                }
            });
    //}

and even ng-click also working

what is wrong with ng-change?

Upvotes: 1

Views: 68

Answers (2)

ram1993
ram1993

Reputation: 971

Change value="comname" in <option ng-repeat="o in comnamelistfun" value="comname"> to either ng-value="comname" or value="{{comname}}"

ng-change not triggering as value doesn't change. You have provided static value "comname".

Upvotes: 1

sisyphus
sisyphus

Reputation: 462

Where is the function call with dynamic variables? Your function has hardcoded values.

Also, before even the function call, try a test function to check if the value of dropdown is being captured or even before that if the function is being called. If the function is being called, and the value is reflecting, you can proceed with a database call.

<select ng-change="`gbrandname`(ucomname)" ng-init="ucomname='comname'" ng-model="ucomname">
                  <option ng-repeat="o in comnamelistfun" value="{{comname}}">{{o.comname}}</option>
              </select>

And then the function call:

var gbrandname = function(ucomname){alert(ucomname.comname)}

You can refer to this plnkr that I just created for you:

Upvotes: 0

Related Questions