M
M
Max2015-05-19 23:06:05
JavaScript
Max, 2015-05-19 23:06:05

Why is the add product form not working?

Hello, please tell me why the product is not added to the JSON file?
Add form:

<form action="">
  Название <input type="text" ng-model="newData.name">
  Описание <input type="text" ng-model="newData.description">
  Страна производитель <input type="text" ng-model="newData.country">
  Калорийность <input type="text" ng-model="newData.calories">
  Цена <input type="text" ng-model="newData.price">
  Термин хранения <input type="text" ng-model="newData.termin">
  Дата изготовления <input type="text" ng-model="newData.data">
  <button ng-click="addProduct()">Добавить новый продукт</button>
</form>

Controller:
productApp.controller('ProductListCntrl',['$scope','$http','$location', function($scope, $http,$location){
  $http.get('products.json').success(function(data, status, headers, config){ // Посылаю запрос для получение данных, если успешно, то
    $scope.products = data; // products заношу все полученные данные
    $scope.addProduct = function(){ // В объект  addProduct записываю значение функции
      $scope.data.push($scope.newData); // в полученную информацию (переменная data) пушу значение newData из формы
      $scope.newData=''; // слабо понимаю что тут происходит)
    };
  });
}]);

JSON example:
[
    {
        "productId": "Milk", 
        "name": "Молоко", 
        "description": "Хорошее молоко",
        "country": "Украина",
        "calories": "220 Ккал",
        "price": 12,
        "termin": 3,
        "data": "12.05.2015"
    },

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Tartmin, 2015-05-20
@maxprof

First, there is the ng-submit directive, and you have a calocode, sorry. Secondly, in order to make an array with an object, you first need to file it as a variable. Describe a specific problem, I will help you.

_
_ _, 2015-05-20
@AMar4enko

Those. do you want to add data to the products.json file with this code? Then I have bad news for you.
UPD:
You have fundamental gaps.
There is no products.json file in your code. Design
requests data from the server using the 'products.json' link, then converts the received data into a JavaScript object, and returns this object to you as the data parameter of the callback function.
You expect that you will have some object in data that is magically associated with the products.json file, by pushing to which you will update this file on the server. But this is not the case, you just get a local object containing the data that the server returned to you in response to a GET request.

@
@codingal, 2015-05-20
_

Your form is not attached to the controller in any way, you need to connect both the entire controller and the addProduct method separately on ng-submit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question