javascript - Angujar JS 表单问题中未定义范围

标签 javascript angularjs data-binding

我在将表单数据(完整的)绑定(bind)到我的 Controller 时遇到问题。下面是我的代码:

<form name="form_foto" novalidate>
        <div class="form-group">
            <label>URL de la foto: </label>
            <!--
            <input type="text" required class="form-control" placeholder="http://ejemplo.com/foto.jpg">
            -->
            <input type="text" required class="form-control" placeholder="http://ejemplo.com/foto.jpg" data-ng-model="mifoto.foto" name="foto">
        </div>
        <div class="form-group">
            <label>Lugar: </label>
            <input type="text" required class="form-control" placeholder="Lugar de ejemplo" data-ng-model="mifoto.lugar" name="lugar">
        </div>
        <div class="form-group">
            <label>Año: </label>        
            <input type="text" required class="form-control" placeholder="2016" data-ng-model="mifoto.anio" name="anio">
        </div>
        <!--
        <button class="btn btn-default">Guardar Foto</button>
        -->
        <button class="btn btn-default" data-ng-disabled="datosNoValidos()" data-ng-click="guardarFoto()">Guardar Foto</button>
    </form>

guardarFoto功能代码:

$scope.guardarFoto = function(){        

        alert($scope.mifoto);   //comes out undefined   

        $scope.misFotos.push($scope.mifoto);
        delete $scope.mifoto;     

    }

任何帮助将不胜感激。谢谢!

最佳答案

只需初始化$scope.mifoto = {}
检查这个实例:

angular.module('app', [])
  .controller('ctrl', ['$scope',
    function($scope) {
      $scope.misFotos = [];
      $scope.mifoto = {};

      $scope.guardarFoto = function() {

        alert($scope.mifoto); //comes out undefined   

        $scope.misFotos.push($scope.mifoto);
        delete $scope.mifoto;

      }
    }
  ])
<!DOCTYPE html>
<html ng-app="app">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-controller="ctrl">
  <form name="form_foto" novalidate>
    <div class="form-group">
      <label>URL de la foto:</label>
      <!--
            <input type="text" required class="form-control" placeholder="http://ejemplo.com/foto.jpg">
            -->
      <input type="text" required class="form-control" placeholder="http://ejemplo.com/foto.jpg" data-ng-model="mifoto.foto" name="foto">
    </div>
    <div class="form-group">
      <label>Lugar:</label>
      <input type="text" required class="form-control" placeholder="Lugar de ejemplo" data-ng-model="mifoto.lugar" name="lugar">
    </div>
    <div class="form-group">
      <label>Año:</label>
      <input type="text" required class="form-control" placeholder="2016" data-ng-model="mifoto.anio" name="anio">
    </div>
    <!--
        <button class="btn btn-default">Guardar Foto</button>
        -->
    <button class="btn btn-default" data-ng-disabled="datosNoValidos()" data-ng-click="guardarFoto()">Guardar Foto</button>
  </form>
</body>

</html>

关于javascript - Angujar JS 表单问题中未定义范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112135/

相关文章:

javascript - Angular ng-table 没有加载数据集?

wpf - 如何在 XAML 中将一个控件的引用传递给另一个控件

javascript - 为什么是下面的||跳过有效值?

javascript - 如何将 momentjs 添加到缩小的 bundle 文件中?

jquery - 编译指令导致错误 : $apply already in progress

objective-c - cocoa 奥特莱斯表现得很奇怪,无法识别选择器

wpf - WPF 数据绑定(bind)是否让事情变得比它的值(value)更痛苦?

javascript - 获取在 javascript 中提交的表单的实例

javascript - 我的新 Reactjs 应用程序出现 404 错误,它是否正在寻找 main.js 文件?

javascript - 是否有一种松散耦合的方式来从动态创建的 View / Controller 更新父指令,该 View / Controller 是父指令的子指令