Angular表单上的TypeError提交

时间:2017-05-24 15:13:12

标签: angularjs

我在尝试向firebase中的某行提交更新时收到错误TypeError: Cannot set property 'songname' of null。控制台错误说它在编辑功能中与record.songname一起发生。我可以添加,但不能编辑行。

myApp.controller('ProductsCtrl', ['$scope', '$firebaseArray', function($scope, $firebaseArray, $http){

  var myProducts = new Firebase('https://url-to-db.firebaseio.com/songs');

  $scope.products = $firebaseArray(myProducts);

  $scope.showForm = function(){
    $scope.addFormShow = true;
    $scope.editFormShow = false;
    clearForm();
    window.scrollTo(0, 0);
  }

  $scope.hideForm = function(){
    $scope.addFormShow = false;
  }

  function clearForm(){
    $scope.songname = '';
    $scope.artist = '';
    $scopt.tags = '';
  }

  $scope.addFormSubmit = function(){
    $scope.products.$add({
      songname:$scope.songname,
      artist:$scope.artist,
      tags:$scope.tags,
      date:Date.now()
    });
    $('.messages').addClass('alert alert-success').slideDown().show().html('The song has been added').fadeOut(2000);
    clearForm();
  }

  $scope.dateFormat = 'MM-dd-yy @ HH:mm:ss';

  $scope.showProduct = function(product){
    $scope.editFormShow = true;
    $scope.addFormShow = false;
    $scope.songname = product.songname;
    $scope.artist = product.artist;
    $scope.tags = product.tags;
    $scope.date = product.date;
  }

  $scope.editFormSubmit = function(){
    var id = $scope.id;
    var record = $scope.products.$getRecord(id);
    record.songname = $scope.songname;
    record.artist = $scope.artist;
    record.tags = $scope.tags;

    $scope.products.$save(record);
    $('.messages').addClass('alert alert-info').slideDown().show().html('The job has been edited').fadeOut(2000);
    clearForm();
    $('.edit-form').toggle();
  }
}]);

1 个答案:

答案 0 :(得分:0)

$ scope.id属性可能未设置。 尝试在showProduct函数的范围内设置id,或者使用哪个函数初始化项目的$ scope变量。

$scope.showProduct = function(product){
$scope.id = product.id;
..............