0

New to Angular JS,

I trying to define custom directive for dropdown with data from web service.

Now trying to do with some static data but not working.

app.directive("deviceBranch", function(){
    return {
        templateUrl : 'resources/view/template/device-branch-list.html',
        controller : function($scope) {
            $scope.listBranch = [{
                deviceBranchId:"id1",
                deviceBranchName:"value1"
            },{
                deviceBranchId:"id2",
                deviceBranchName:"value2"
            },{
                deviceBranchId:"id3",
                deviceBranchName:"valkue3"
            }]
        }
    }
});

Added "Select" in the template with-

ng-options="service.deviceBranchId as service.deviceBranchName for service in listBranch"

This is how I am using directive

<device-branch></device-branch>

Select is populated, but no options are available.

Please assist.

2
  • I would like to populate options from webservice Commented Apr 20, 2018 at 9:45
  • can you post your template ? or even better a plker reproducing your problem Commented Apr 20, 2018 at 12:40

1 Answer 1

1

Looks like you were just missing an ng-model in your template. Below is a link to a working sample.

<select ng-options="service.deviceBranchId as service.deviceBranchName for service in listBranch" ng-model="selected"></select>

You can also inject $http into your directive to get data from web services.

app.directive("deviceBranch", function($http){
  return {
    templateUrl : 'device-branch-list.html',
    scope: {
      ngModel: '='
    },
    controller : function($scope) {
        $scope.listBranch = [{
            deviceBranchId:"id1",
            deviceBranchName:"value1"
        },{
            deviceBranchId:"id2",
            deviceBranchName:"value2"
        },{
            deviceBranchId:"id3",
            deviceBranchName:"valkue3"
        }]
    },
    link: function($scope, element, attrs){
      //var url = ""
      //$http.get(url).then(function(response){
      // $scope.listBranch = response;
      //})
    }
  }
});

Here's the template (device-branch-list.html):

<select ng-options="service.deviceBranchId as service.deviceBranchName for service in listBranch" ng-model="ngModel">

https://plnkr.co/edit/essg24GsrhSKwqXO578p?p=preview

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

2 Comments

Thanks for the answer, but I would like to give custom model name, because this directive can be use more than once on same page. Please try to give update
I've updated the plnkr so that you could add ng-model onto the directive. The variable you pass will now two way bind with the input as seen in the demo. The changes includes adding 'ng-model' to script.js and index.html. Also a change on the template html file to change from 'selected' to 'ngModel'

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.