角度表达不适用于ng-disabled

时间:2016-07-08 18:15:33

标签: angularjs angularjs-directive

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>

<body ng-app="myApp">
<div ng-controller="toDoCtrl">
   <div>
    <hr>
    <form class="form" name="commentForm" ng-submit="vm.submitCommentForm()">

        <div class="form-group">
            <label for="fieldBody">Comment:</label>
                        <textarea name="body" id="fieldBody" class="form-control" rows="3"
                               ng-change="vm.changeCommentForm()"
                               ng-model="vm.commentForm.body"></textarea>
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary" ng-disabled="!vm.validCommentForm">Comment</button>
        </div>
    </form>
    <hr>
</div>
</div>

脚本

    var app = angular.module('myApp', []);
    app.controller('toDoCtrl', function () {
    var vm = this;
    vm.validCommentForm = true;
    })

&#34;评论&#34; vm.validCommentForm = true时,按钮显示已禁用 同时,如果我硬编码ng-disabled=0注释按钮出现工作,如假设。我出了什么问题?

1 个答案:

答案 0 :(得分:3)

您使用的是controllerAs方法,但未在视图中设置别名

尝试更改

<div ng-controller="toDoCtrl">

<div ng-controller="toDoCtrl as vm">
相关问题