从Django插入模态表单时,ng-model没有附加到controller $ scope

时间:2016-07-07 12:20:08

标签: angularjs django django-forms angular-ui-bootstrap django-angular

我使用template属性将Django的表单提供给angular-ui-bootstrap模式:

function openFinderModal() {
    var modalInstance = $modal.open({
        template: vm.modalTemplate,
    });

模式模板的相关部分从表单视图提供:

<div class="modal-body" >
    <form ng-controller="ServiceFinderModalController">
        {% csrf_token %}
        {{ form }}
    </form>
</div>

然后我使用django-angular在每个表单字段上创建ng-model(请参阅docs),对应下面的简单表单:

class ServiceSelectionForm(NgModelFormMixin, NgForm):
    service_0 = forms.BooleanField()

但是,当我尝试从$scope访问此表单字段时,我得到undefined

function ServiceFinderModalController($scope) {
    console.log($scope.service_0);
}

我已经检查了页面的来源,看起来好像应该有效(见最后一行):

<form ng-controller="ServiceFinderModalController" class="ng-pristine ng-valid ng-scope">

  <input type="hidden" name="csrfmiddlewaretoken" value="dkH6aiHyIqqkJlN5xAbiYaX0c37tXb9U">
  <ul class="djng-form-errors ng-hide" ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.$pristine">
    <li ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.$message" class="invalid ng-binding ng-hide" ng-bind="U2VydmljZVNlbGVjdGlvbkZvcm0.$message"></li>
  </ul>
  <ul class="djng-form-errors ng-hide" ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.$dirty"></ul>
  <label for="id_service_0">Test 2</label>
  <ul class="djng-field-errors ng-hide" ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.service_0.$pristine">
    <li ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.service_0.$message" class="invalid ng-binding ng-hide" ng-bind="U2VydmljZVNlbGVjdGlvbkZvcm0.service_0.$message"></li>
  </ul>
  <ul class="djng-field-errors ng-hide" ng-show="U2VydmljZVNlbGVjdGlvbkZvcm0.service_0.$dirty"></ul>
  <input id="id_service_0" name="service_0" ng-model="service_0" type="checkbox" class="ng-pristine ng-untouched ng-valid ng-empty">

</form>

我尝试过像$scope.$digest()这样的事情,但却出现[$rootScope:inprog]错误。

更新

$scope切换后会发现此字段。现在我只需要找出一种方法来实现这一点......

0 个答案:

没有答案