SK-USA
SK-USA

Reputation: 103

Unable to invoke controller function from ng-dropdown-multiselect directive

I need to bind an event in my controller to be called when there is a change in value of a dropdown. This dropdown is a multi-select dropdown -which is in a directive.

I have been able to customize the dropdown using the settings available - however, unable to bind the event.

Here is the code HTML:

    <html>
    <head>
        <meta charset="utf-8">
        <title>AngularJS Dropdown Multiselect</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <script src="./angular.min.js"></script>
        <!--<script src="./ui-bootstrap-tpls.js"></script>-->
      <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
      <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.7.0/lodash.min.js"></script>
        <script src="./app.js"></script>
        <style>
            .noscroll {
                overflow: hidden;
            }
        </style>

    </head>
    <body>
<div ng-app="myApp" ng-controller="AppCtrl">
    <div ng-dropdown-multiselect="" options="fruitdata"
         selected-model="fruitmodel" checkboxes="true"
         extra-settings="fruitsettings" events="extraEvents">
    </div>
    Selected Model: {{fruitmodel}} | json

</div>
    </body>
</html>

JS:

    'use strict';

var app = angular.module('myApp', ['angularjs-dropdown-multiselect']);

app.controller('AppCtrl', function ($scope) {
    $scope.fruitmodel = [];
    $scope.fruitsettings = {
        enableSearch: false
    };
    $scope.fruitdata = [{
        "label": "Apple",
            "id": 1
    }, {
        "label": "Banana",
            "id": 2
    }, {
        "label": "Orange",
            "id": 3
    }, {
        "label": "Pineapple",
            "id": 4
    }, {
        "label": "Strawberry",
            "id": 5
    }];
    $scope.example2settings = {
        displayProp: 'id'
    };

    $scope.extraEvents = {
        onItemDeselect: '$scope.onChange'
    };

    $scope.onChange = function() {
        console.log($scope.fruitdata);
    };
});

I need to detect the change to the model and take action based on this. I tried two approaches - adding ng-change to the dropdown as below - however, this does not work.

<div ng-dropdown-multiselect="" options="fruitdata"
     selected-model="fruitmodel" checkboxes="true"
     extra-settings="fruitsettings" ng-change="onChange()">
</div>

The directive does seem to allow extending the event handling - however, I guess I am not getting it right.

Upvotes: 0

Views: 727

Answers (2)

Fabio Ruggiero
Fabio Ruggiero

Reputation: 309

Snapshot from AngularJS Dropdown Multiselect

enter image description here

Seems like you have to use:

<div ng-dropdown-multiselect="" ... events="eventsCallback"></div>

and into your controller:

$scope.eventsCallback = {
    onItemSelect:       function () { ... },
    onSelectionChanged: function () { ... }
}

Upvotes: 0

Dillon
Dillon

Reputation: 707

Is $scope.fruitmodel where you are storing the selected items? You can use $scope.$watchCollection to act on changes.

$scope.$watchCollection('fruitmodel', function(newValue, oldValue) {
    console.log(newValue, oldValue);
});

Upvotes: 0

Related Questions