gpt4 book ai didi

javascript - 带空格的 AngularJS 自定义指令参数

转载 作者:数据小太阳 更新时间:2023-10-29 05:04:17 25 4
gpt4 key购买 nike

我有一个自定义指令,用于标准化日期输入并格式化它们以匹配我的(有点奇怪的)API 要求。用于调用它的标签如下:

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

我收到以下错误:

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

当我删除空格时(即 date-label="DateOfBirth" 它工作正常。)

如何在指令属性中允许空格?

指令:

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'
}
}
}]);

最佳答案

将属性值传递给指令时不应该使用@吗?

scope: {
'dateLabel': '@dateLabel'
}

关于javascript - 带空格的 AngularJS 自定义指令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16715841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com