AngularJS不会在select中解析ng-disabled

时间:2013-09-09 08:24:44

标签: javascript html angularjs

以下所有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属性,则可以正常使用。

1 个答案:

答案 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>