0

I have written a directive that as part of the functionality, reads a dropdown menu and passes the value back to an API. After investigating the network information, I've determined it's not detecting the change in values, and is always just passing the default value of "none". Unfortunately I cannot set up a demo of it, but I was hoping someone could look at my code snippet below and tell me where my error might lie. Thank you all so much in advance.

JS:

angular.module('web')

.directive('alertSettingsPanel', function (config, $window, moment, $timeout, $http, SimpleModal) {
    'use strict';

    return {
        restrict: 'E',
        scope: {
            filter: '=alertSettingsPanel',
            department: '='
        },
        templateUrl: config.moduleRoots['web'] + '/AlertSettings/templates/partial.alertSettingsPanel.min.html',
        link: {
            pre: function (scope) {

                console.dir(scope);

                //initialize save text
                scope.saved = "Save Filter";
                scope.validate = false;

                // Collapse the body if not new
                scope.UICollapseBody = scope.filter.id === 'new';
                scope.UIBodyExpanded = scope.UICollapseBody;
                /**
                 * Toggles the display of the body
                 */
                scope.UIToggleBody = function () {
                    var nextCollapse = !scope.UICollapseBody;
                    scope.UICollapseBody = nextCollapse;
                    $timeout(function () {
                        scope.UIBodyExpanded = nextCollapse;
                    }, 300);
                };

                //sends filter data to API for saving
                scope.save = function(){
                    scope.saved = "Saving...";
                    if(scope.filter.id === 'new'){
                        $http.post(config.apiBase + 'users/me/shifts/' + scope.$parent.shifts[scope.$parent.selectedShiftIndex].id + '/filters', {
                            sendFollowup: scope.filter.sendFollowup,
                            alert: {fileName: "default", type: scope.filter.alert.type},
                            type: scope.filter.type,
                            description: scope.filter.description,
                            priority: scope.filter.priority,
                            value: scope.filter.value
                        }).then(function(response){
                            scope.filter.id = response.data.id;
                            scope.saved = "Saved!";
                        }, function(){
                            scope.saved = "Error saving";
                        });
                    }
                    else{
                        $http.put(config.apiBase + 'users/me/shifts/' + scope.$parent.shifts[scope.$parent.selectedShiftIndex].id + '/filters/' + scope.filter.id, {
                            sendFollowup: scope.filter.sendFollowup,
                            alert: {filename: scope.filter.alert.filename, type: scope.filter.alert.type},
                            type: scope.filter.type,
                            description: scope.filter.description,
                            priority: scope.filter.priority,
                            value: scope.filter.value
                        }).success(function(){
                            scope.saved = "Saved!";
                        }).error(function(){
                            scope.saved = "Error saving";
                        });
                    }
                };

                //Removes filter from local array
                //Note: Only use when the filter no longer exists on the server
                scope.uncreate = function() {
                    scope.$parent.uncreateFilter(scope.$parent.filters.indexOf(scope.filter));
                };


                //Removes filter from shift in API
                scope.remove = function() {
                    SimpleModal.open({
                        title: 'Remove filter?',
                        body: 'Are you sure you want to remove this filter from your department?',
                        type: 'confirm',
                        buttons: {
                            ok: 'Yes, remove this filter',
                            cancel: 'Cancel'
                        }
                    }).then(function() {
                        //REMOVE FROM SERVER
                        $http.delete(config.apiBase + 'users/me/shifts/' + scope.$parent.shifts[scope.$parent.selectedShiftIndex].id + '/filters/' + scope.filter.id)
                            .then(
                            //remove locally
                            scope.uncreate()
                        );
                    });
                };
            }
        }
    };
});

HTML:

<form name="filtersForm" ng-submit="save()" autocomplete="off">
    <div class="row">
        <div class="col-md-5">
            <h4><i class="fa fa-info-circle"></i> <strong>Filter Information</strong></h4>
        </div>
    </div>
    <div class="row" style="border-top: 1px solid #ddd; padding-top: 10px">
         <div class="col-md-12">
             <div class="form-horizontal">
                 <div class="col-md-6">
                     <div class="form-group">
                         <label for="description" class="col-sm-4 control-label">Description </label>
                         <div class="col-sm-8">
                             <input ng-disabled="filter.priority<1"  id="description" ng-model="filter.description" class="form-control" required>
                          </div>
                     </div>
                     <div class="form-group">
                         <label for="sendFollowup" class="col-sm-4 control-label">Followup</label>
                         <div class="col-sm-8">
                             <div class="checkbox normal-text">
                                 <label>
                                      <input type = "checkbox" id="sendFollowup" ng-model="filter.sendFollowup">
                                            Send followup?
                                  </label>
                              </div>
                          </div>
                      </div>
                  </div>
                  <div class="col-md-6">
                      <div job-type-search="filter" model-type="filter" validate="validate" autocomplete="off"></div>
                            <div class="form-group">
                                <label for="alertType" class="col-sm-4 control-label">Alert Type: </label>
                                <div class="col-sm-8">
                                    <select class="form-control" id="alertType" ng-model="filter.alert.type" convert-to-string required>
                                        <option value="none">No Alert</option>
                                        <option value="silence">Vibrate Alert</option>
                                        <option value="audio">Audio Alert</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" style="border-top: 1px solid #ddd; padding-top: 10px">
                <div class="col-md-3 col-md-offset-4">
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-block" ng-class="{'btn-success': saved === 'Saved!'}" ng-disabled="saved === 'Saving...' || filtersForm.$invalid || (!validate && filter.value!==null)">{{saved}}</button>
                    </div>
                </div>
            </div>
        </form>
M. Lynch
  • 1
  • 1
  • How do you use this directive ? I saw conceptual problem in your directive. In your save method you use $parent in scope to get some values but you defined directive scope to isolate. You break the isolation to do this. – Silvinus Jun 06 '16 at 15:10
  • @Silvinus This directive is paired with a settings page. Each time this directive is instantiated it is for an individual setting. Settings are applied to a specific criteria that is set by the user. If that criteria is met, they are alerted. The issue I am currently having is that scope is undefined inside the pre. I'll admit I am new to angular so I'm not quite sure what I did wrong with $parent. I just know I'm not supposed to use $child inside of a pre. – M. Lynch Jun 07 '16 at 14:07
  • @Silvinus Okay after some reading I understand what you mean. How can I access the parent with my isolated scope without breaking it? I need to be able to access those variables. Should I define them within my scope when I create it or can I access the controller values some other way? I figure my issue is because it's breaking the isolated scope, that is the reason why it's sending just the default value (the value seen in the controller) as opposed the new value that the dropdown is at (what is seen in the directive). Does this make sense? – M. Lynch Jun 07 '16 at 16:29

0 Answers0