Javascript 如何将表单字段作为ng include包含在表单中?

Javascript 如何将表单字段作为ng include包含在表单中?,javascript,forms,angularjs,single-page-application,Javascript,Forms,Angularjs,Single Page Application,对Angular相对较新,正在尝试创建一个用于创建和编辑操作的窗体。我希望共享一些表单字段,但据我所知,ng include创建了一个新范围,因此我的字段(使用ng model)不绑定到原始范围。结果是,当表单提交时,我的字段不会被发送进来 如果我走错了方向,请告诉我任何可能有帮助的文档。我的部分问题是,我不确定从哪里看这一点。谢谢 创建表单 新专辑 形成局部 名称 相册图片 发布日期 SKU 在我看来,你做得对。您可能需要在子作用域上使用它之前,在$scope上定义$scope.albu

对Angular相对较新,正在尝试创建一个用于创建和编辑操作的窗体。我希望共享一些表单字段,但据我所知,
ng include
创建了一个新范围,因此我的字段(使用
ng model
)不绑定到原始范围。结果是,当表单提交时,我的字段不会被发送进来

如果我走错了方向,请告诉我任何可能有帮助的文档。我的部分问题是,我不确定从哪里看这一点。谢谢

创建表单

新专辑
形成局部

名称
相册图片
发布日期
SKU

在我看来,你做得对。您可能需要在子作用域上使用它之前,在
$scope
上定义
$scope.album

$scope.album = {}; // set it on your AlbumsController
更改:


to:

我和您有同样的问题,在我的解决方案中,我使用父作用域在子视图绑定数据。像这样:

<fieldset class="well">
  <div class="form-group">
    <label class="control-label" for="name">Name</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.name" id="name" class="form-control" placeholder="Name" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="name">Album Picture</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.picture" id="picture" class="form-control" placeholder="Album Picture" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="releaseDate">Release Date</label>
    <div class="controls">
      <input type="date" data-ng-model="$parent.album.releaseDate" id="releaseDate" class="form-control" placeholder="Release Date" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="sku">SKU</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.sku" id="sku" class="form-control" placeholder="SKU" required>
    </div>
  </div>
</fieldset>

名称
相册图片
发布日期
SKU

这很难看,但工作对我有好处。

$parent在紧要关头工作,但使用它需要知道父范围/上下文。太棒了。谢谢再加上在控制器中添加作用域(如so-
var track=new Tracks($scope.track);
),世界上发生了巨大的变化。谢谢
$scope.album = {}; // set it on your AlbumsController
<fieldset class="well">
  <div class="form-group">
    <label class="control-label" for="name">Name</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.name" id="name" class="form-control" placeholder="Name" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="name">Album Picture</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.picture" id="picture" class="form-control" placeholder="Album Picture" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="releaseDate">Release Date</label>
    <div class="controls">
      <input type="date" data-ng-model="$parent.album.releaseDate" id="releaseDate" class="form-control" placeholder="Release Date" required>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label" for="sku">SKU</label>
    <div class="controls">
      <input type="text" data-ng-model="$parent.album.sku" id="sku" class="form-control" placeholder="SKU" required>
    </div>
  </div>
</fieldset>