如何清除ng-include中的表单

时间:2014-08-27 04:38:25

标签: javascript angularjs

我有一个带有可重复使用模式的html模板,其包含如下所示:

myPartial.html

... SOME HTML ...
<ng-include src="'form.html'"></ng-include>

modal.html

...
<form>
  <input ng-model="comment.value" />
  <button type="submit" ng-click="save(comment.value)" />
</form>
...

清除表单的最佳方法是什么 - 特别是如果可能的话,从控制器中删除。我想在成功进行http调用后从我的save()方法中清除表单,但由于ng-include创建了自己的范围,我不知道该怎么做。

这是一个显示我的问题的傻瓜:http://plnkr.co/edit/zkxZOZr3f7sHJZfCiuoT?p=preview

如果您获取form.html的内容并将它们直接放入index.html,它就可以正常工作。

1 个答案:

答案 0 :(得分:1)

你几乎得到了它。只需初始化控制器中的注释实例,并在保存中更新正确的值。我相信你的体验被称为“原型继承”,你可以在Understanding scopes阅读它。这不是AngularJS btw独有的,但也可以在常规JavaScript中找到。

angular.module('app', []).controller('appcontroller', function($scope) {

  // create comment
  $scope.comment = { value: 'test' };
  $scope.save = function(value) {
    console.log('in here ');

    // and update comment.value, not comment.
    $scope.comment.value = null;
  };
});

我更新了您的plunker