首页 > 解决方案 > 如何使用 PHP Codeigniter 将数据输入 Angular 插入到 MongoDB

问题描述

我开始使用 Angular Framework、MongoDB 和 Codeigniter。我想问一下提交的Angular基本输入表单是名称:电子邮件:地址:。我希望将三个字段的数据插入 MongoDB。数据不直接转到 MongoDB,首先将数据传输到 PHP Codeigniter 和字段名称,然后转到数据库(MongoDB)。我已经在使用 Nodejs。

请告诉我什么是实际的 Angular 数据传输代码或数据插入代码。


分支列表.component.ts

      name: {
        title: 'Branch-Name',
        width: '40%'
      },
       Address: {
        title: 'Address',
        width: '40%'
      },
    }
  };

  constructor(private router: Router, private generalApiService: GeneralApiService
    , private route: ActivatedRoute, private modalService: NgbModal) { }

  ngOnInit() {
    this.generalApiService.getAll(this.apiUrl).subscribe(res => {
      this.branch = res;
    });
  }
---------------------------------------------------------------
pages.module.ts
---------------------------------------------------------------
import { NgModule } from '@angular/core';

import { PagesComponent } from './pages.component';
import { DashboardModule } from './dashboard/dashboard.module';
import { PagesRoutingModule } from './pages-routing.module';
import { ThemeModule } from '../@theme/theme.module';
import { MiscellaneousModule } from './miscellaneous/miscellaneous.module';
import { ModalComponent } from './components/modal.component';

const PAGES_COMPONENTS = [
  PagesComponent,
  ModalComponent
];

@NgModule({
  imports: [
    PagesRoutingModule,
    ThemeModule,
    DashboardModule,
    MiscellaneousModule,

  ],
  entryComponents:[
    ModalComponent
  ],
  declarations: [
    `...PAGES_COMPONENTS,`
  ],
})
export class PagesModule {
}

----------------------------------------------------------------------
`mongo_db.php in CI
---------------------------------------------------------------------
$config['mongo_db']['active'] = 'default';

$config['mongo_db']['default']['no_auth'] = true;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = '';
$config['mongo_db']['default']['password'] = '';
$config['mongo_db']['default']['database'] = 'myancon';
$config['mongo_db']['default']['db_debug'] = true;
$config['mongo_db']['default']['return_as'] = 'array';
$config['mongo_db']['default']['write_concerns'] = (int) 1;
$config['mongo_db']['default']['journal'] = true;
$config['mongo_db']['default']['read_preference'] = 'primary';
$config['mongo_db']['default']['read_concern'] = 'local'; //'local', 'majority' or 'linearizable'
$config['mongo_db']['default']['legacy_support'] = false;

------------------------------------------------------------------
branch.php Controller in CI
-------------------------------------------------------------------
class Branches extends REST_Controller
{
  public function index_post() {
        if ($this->input->method(TRUE) === 'POST') {
            $_POST = json_decode(file_get_contents('php://input'), true);
            $data = array(
                "branchname" => $this->input->post('branchname')
                ,"address" => $this->input->post('address')
                ,"phone" => $this->input->post('phone')
        );

            if ($this->input->post('_id') != null) {
                $_id = $this->input->post("_id");

            } else {
                $this->Branch_model->add($data);
            }

            $this->response([], 201);
        }
    }
}

---------------------------------------------------------------------
Branch_model.php
--------------------------------------------------------------------
class Branch_model extends CI_Model
{
 public function add($data) {
        return $this->`mongo`_db->insert("branch", $data);
    }
}

Angular 数据连接代码和数据插入代码请用 Angular 文件名解释!

`

标签: angularmongodbcodeigniterinsert

解决方案


推荐阅读