【问题标题】:Angular JS send html to data baseAngular JS 将 html 发送到数据库
【发布时间】:2019-12-13 18:27:18
【问题描述】:

我正在发送html

你好

<div class="input-field">
 <input id="price" type="number" ng-model="productData.price" ng-init="productData.price = ''" >
  <label for="price">Price</label>
</div>



<div id="editor3" class="ql-container ql-snow">
      <div class="ql-editor" data-gramm="false" contenteditable="true">
        <p>hello</p>
      </div>
    </div>
    <input type="hidden" ng-model="productData.description">

到 Angular js 代码,但它不起作用。我是角度 js 的新手。下面的第五行我不从 HTML 代码中获取数据

 $scope.addProdcut = function (productData) {
        if ($scope.validateProduct(productData)) {
            $scope.productLoader = true;
            $('#productSubmit').attr('disabled', true);
$scope.productData.description = $('#editor3 .ql-editor').html() != '' ? $('#editor3 .ql-editor').html() : '';
            var url = webroot + 'products/addProduct';
            $scope.data = {
                "type": "json",
                "data": productData,
                "image": $scope.productImage
            };
            $http.post(url, $scope.data, {
                headers: {
                    'X-CSRF-Token': token
                }
            });
        }
    }  

控制器文件

public function addProduct()
{
    if ($this->request->is('post')) {
        $content = $this->request->input('json_decode', true);
        $data = $content['data'];
        $file = '';
        if ($content['image'] != '') {
            $file = date('ymd') . time() . '.' . 'jpg';
            $path = WWW_ROOT . 'img/products/' . $file;
            $this->base64_to_jpeg($content['image'], $path);
        }
        $products = TableRegistry::get('Products');
        $query = $products->query()->insert(['item', 'price', 'description', 'image', 'status', 'added_by', 'created'])
            ->values([
                'item' => $data['name'],
                'price' => $data['price'],
                'description' => $data['description'],
                'image' => $file,
                'status' => 1,
                'added_by' => $this->loggedInUser['id'] . '||' . $this->loggedInUser['username'],
                'created' => date("Y-m-d\TH:i:s"),
            ])->execute();
        echo json_encode(['status' => 'success']);
        exit;
    }
}

它是一种形式的文字编辑器,我想使用隐藏的输入字段将 HTML 代码(来自文字编辑器)保存在数据库中。通过将数据 ng-model 发送到隐藏的输入字段

【问题讨论】:

    标签: javascript php html json angularjs


    【解决方案1】:

    您将值存储在$scope.productData.description 中,然后发送productData,相反,您应该发送$scope.productData.description

    $scope.addProdcut = function(productData) {
      if ($scope.validateProduct(productData)) {
        $scope.productLoader = true;
        $('#productSubmit').attr('disabled', true);
        $scope.productData.description = $('#editor3 .ql-editor').html() != '' ? $('#editor3 .ql-editor').html() : '';
        var url = webroot + 'products/addProduct';
        $scope.data = {
          "type": "json",
          "data": $scope.productData.description,
          "image": $scope.productImage
        };
        $http.post(url, $scope.data, {
          headers: {
            'X-CSRF-Token': token
          }
        });
      }
    }
    

    【讨论】:

    • 我编辑了一个代码,检查唯一的描述是 productData.description 类似的其他是 productData.name,productData.rollno 等
    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2019-08-02
    • 2016-07-20
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多