以下所有HTML代码段在指令模板中解析错误时失败:
1
<select ng-model="model" ng-options="s.abbreviation as s.name for s in states" ng-disabled="false"></select>
失败了:
Error: Syntax Error: Token 'false' is an unexpected token at column 18 of the expression [state.isDisabled false] starting at [false]
2
<select ng-model="model" ng-options="s.abbreviation as s.name for s in states" ng-disabled="disabled"></select>
其中$scope.disabled
是布尔值(false
)失败的:
Error: Syntax Error: Token 'disabled' is an unexpected token at column 18 of the expression [state.isDisabled disabled] starting at [disabled].
3
<select ng-model="model" ng-options="s.abbreviation as s.name for s in states" ng-disabled="isDisabled()"></select>
其中$scope.isDiabled
是函数失败的:
Error: Syntax Error: Token 'isDisabled' is an unexpected token at column 18 of the expression [state.isDisabled isDisabled()] starting at [isDisabled()].
当相同的代码在主HTML中时,它工作正常,但在我将其移动到自己的指令后停止解析ng-disabled
。
这是用于此指令的JavaScript:
myApp.directive("selectState", [
function () {
var states = [ // src: https://gist.github.com/mshafrir/2646763
{ "name": "Alabama", "abbreviation": "AL" } /*, { .. }, { .. }*/
];
return {
templateUrl: "/directives/select-state/select-state.html",
replace: true,
scope: { model: "=", isDisabled: "=" },
restrict: "EA",
controller: function ($scope, $element, $attrs) {
$scope.states = states;
$scope.disabled = false;
if (!$scope.isDisabled) {
$scope.isDisabled = function () { return false };
}
}
}
}
]);
它出了什么问题?如果我删除ng-disabled
属性,则可以正常使用。
答案 0 :(得分:2)
我明白了,包含HTML也有ng-disabled
而不是is-disabled
因此,看起来似乎(但我可能错了),AngularJS连接了两个ng-disabled
属性。我没有花太多时间研究 如何出错,但这肯定是错误的原因。
我通过改变它来修复它:
<select-state model="myModel" ng-disabled="myFunc"></select-state>
到此:
<select-state model="myModel" is-disabled="myFunc"></select-state>