javascript - Angularjs: How to bind different result set with different controls using common method? -
i new angularjs , facing 1 problem.
the requirement little bit unique in itself, here description:
i have 2 inputs, 2 dropdown , 2 buttons , 1 time come in 1 url in first input field , click on button, bind "key" not values dropdown using "getapiresults" method in controller when add together other url in sec input field , click on button fetch keys sec api , bind dropdown problem bind first dropdown 1 time again sec api results because of fact updating "fieldnames". don't know how handle problem.
please help.. here finish code:
<html lang="en">
<div ng-controller="hellocontroller"> <div> <input ng-model="url1" style="width:400px" /> <button class="btn btn-danger" data-ng-click="getapiresults(url1)"> </button> <select class="form-control" ng-init="getapiresults(url1)" data-ng-options="key key (key,value) in fieldnames[0]" ng-model="selected"></select> </div> <br /> <br/> <div> <input ng-model="url" style="width:400px" /> <button class="btn btn-danger" data-ng-click="getapiresults(url)"> </button> <select class="form-control" ng-init="getapiresults(url)" data-ng-options="key key (key,value) in fieldnames[0]" ng-model="selected1"></select> </div> </div> <script> angular.module("myapp", []) .controller("hellocontroller", function ($scope, $http) { $scope.getapiresults = function (apiurl) { $scope.fieldnames = []; var serviceurl = apiurl; $http.get(serviceurl).success(function (data) { $scope.fieldnames = data; }); }; }); </script>
they both pointing same scope variable. can create little alter getapiresults
function:
$scope.getapiresults = function (dropdown, apiurl) { $scope.fieldnames = []; var serviceurl = apiurl; $http.get(serviceurl).success(function (data) { $scope.fieldnames[dropdown] = data; });
and on html:
<button class="btn btn-danger" data-ng-click="getapiresults('dropdown1',url1)"> </button> <select class="form-control" ng-init="getapiresults('dropdown1',url1)" data-ng-options="key key (key,value) in fieldnames['dropdown1']" ng-model="selected"></select>
javascript jquery angularjs
No comments:
Post a Comment