Ng-Click在Ng-If

时间:2016-07-26 15:57:11

标签: angularjs

编辑:我发现我的Ng-click在我的Ng-if中没有工作。我该怎么做才能解决这个问题?

原帖:

在搜索完其他答案之后,我对为什么这不起作用感到困惑。它正在进行事先提交,但我无法理解为什么我的ngclick甚至没有激活。我在ngclick函数中有控制台打印注释。我甚至没有500错误或任何意味着addItem()没有激活...

查看

<form class="row container center" name="itemForm" ng-submit="addItem()">

  <div class="center ">
        <br>
            <input ng-model="item.title" name="title" type="text" class="form-control validate" placeholder="Item Name">
        <br>
            <input ng-model="item.city" name="city" type="text" class="form-control validate" placeholder="Amount">
            <input ng-model="item.state" name="state" type="text" class="form-control validate" placeholder="Amount">

          <a class="waves-effect waves-light btn center white-text" ng-click="addItem()" type="submit" ng-submit="addItem()">Add Item</a>
    </div>

</form>

控制器

为其他事情工作。 addItem从上面的表单中获取东西,然后也使用userId,因为下面的模型与userId相关联。

angular.module("mvpApp")
.controller('dashboardCtrl', ['$scope', '$http', '$location', function($scope, $http, $location) {

  $scope.init = function() {
    setTimeout(function() {
      $scope.checkAuthentication();
      $scope.getUserItems();
      $scope.getItems();
    },100);
  }

  $scope.getUserItems = function() {
    $http.get('/api/items?UserId=' + $scope.user.id)
    .then(function(result) {
      $scope.userItems = result.data;
      for(var i = 0; i < $scope.userItems.length; i++) {
        $scope.userItems[i].newActivity = {};
      }
    }, function(err) {
      console.log(err)
    });
  };

  $scope.addItem = function(){
    $http.post("/api/items", {
      title: $scope.item.title,
      city: $scope.item.city,
      state: $scope.item.state,
      UserId: $scope.user.id
    })
    .then(function (result) {
      $scope.userItems.push(result.data);
      $scope.item.title = "";
      $scope.item.city = "";
      $scope.item.state = "";
     },function(err) {
      console.log(err)
    });
  };

}]);

模型

module.exports = function(sequelize, DataTypes) {
  var Item = sequelize.define('Item', {
    title: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    city: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    state: {
      type: DataTypes.STRING
    }

  }, {
    classMethods: {
      associate: function(models) {
        Item.belongsTo(models.User)
      }
    }
  });

  return Item;
}

0 个答案:

没有答案