Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Custom Directive Argument with Spaces

I've got a custom directive used to standardize date inputs and format them to match my (somewhat strange) API requirements. The tag used to invoke it is as follows:

<date-input date-id="birthDate" date-label="Date Of Birth" ng-model="client.dateOfBirth"></date-input>

I'm getting the following error:

Syntax Error: Token 'Of' is an unexpected token at column 6 of the expression [Date Of Birth] starting at [Of Birth].

When I remove the spaces (i.e date-label="DateOfBirth" it works fine.)

How can I allow spaces in directive attributes?

The directive:

directives.directive('dateInput', [function() {
  var link = function(scope, element, attrs, model) {
    scope.dateLabel = attrs.dateLabel;
    scope.dateId = attrs.dateId;

    var dateObjectPre = moment(scope.dateObject);
    scope.dateObjectPre = dateObjectPre.format('MMDDYYYY');

    scope.update = function() {
      var dateObject;
      if(angular.isDefined(scope.dateObjectPre)) {
        dateObject = moment(scope.dateObjectPre, 'MMDDYYYY');
      }

      if (dateObject && dateObject.isValid()) {
        scope.dateObject = dateObject.format('YYYY-MM-DD');
      }
      else {
        scope.dateObject = '';
      }
    };
  };

  return {
    restrict: 'E',
    link: link,
    templateUrl: '/views/directives/dateInput.html',
    replace: true,
    scope: {
      'dateLabel': '=dateLabel',
      'dateObject': '=ngModel',
      'dateShow': '=dateShow',
      'dateRequired': '=dateRequired',
      'dateId': '=dateId'
    }
  }
}]);
like image 985
jdp Avatar asked May 23 '13 13:05

jdp


1 Answers

Shouldn't you be using @ when passing attribute values into directives?

scope: {
      'dateLabel': '@dateLabel'
}
like image 164
Jani Hyytiäinen Avatar answered Sep 18 '22 00:09

Jani Hyytiäinen