0

在指令模板中时,以下所有 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属性,它工作正常。

4

1 回答 1

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>
于 2013-09-10T08:59:26.263 回答