1

I have made one simple HTML file in angular. I want to connect my form to MongoDB.

I have attached my "html file" and "JS File".

HTML FIle

var app = angular.module('example',[]);

app.controller('TestController', function(){

	$scope.user.name= "Hello";
	$scope.user.mail = "[email protected]";
});
<html>
</!DOCTYPE html>
<html>
<head>
	<title>TEST Page</title>

<!-- begin snippet: js hide: false console: true babel: false -->

</head>
<body ng-app="example" ng-controller="TestController">
<table>
    <tr>
        <td>Name:</td>
        <td><input type="text" ng-model="user.name"></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><input type="text" ng-model="user.mail"></td>
    </tr>
    <tr>
        <td colspan="2"><button ng-click="getData()"></button></td>
    </tr>
</table>
</body>
</html>

Please help me......

5
  • you have to use a server side language like php to do that. and you have to use $http to connect to server side language Commented Sep 23, 2016 at 6:12
  • you have to write server side code to do that or you can use some BAAS like firebase or loopback Commented Sep 23, 2016 at 6:19
  • Can you Just Elaborate your explanation by running the Code Snippet Commented Sep 23, 2016 at 6:30
  • I have also installed and configured MongoDB in my System. Commented Sep 23, 2016 at 6:31
  • You need a server with support for mongodb driver to make entry into Mongo. In short post the entries into a running server and server will have a connection to mongoDB where it will insert the entries into collection Commented Sep 23, 2016 at 7:20

3 Answers 3

1

If you start mongod with the --rest parameter you can access your DB collections via RESTful endpoints like:

http://ip:port/database/collection/?filter_name=filter_param

So you can make a AJAX request and get the required data as JSON

For more details check the https://docs.mongodb.com/ecosystem/tools/http-interfaces/#rest-interfaces

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this but I am getting the message as "It looks like you are trying to access MongoDB over HTTP on the native driver port." I am having the collections, and inside the collections I have also defined some values.
0

To get data from mongodb you need to have a server (with your mongo model) which authorizes the client to grep the data and returns it to the client.Because we don't trust the client.

Your approach is to make a serverless application. You can use angularjs with firebase for exemple.

martin fowler serverless articles

Comments

0

It seems that you are new in AngularJs and MongoDB.

Let me try to explain how they all works.

Communication with $http To get / post data from / to server.

var app = angular.module('example',[]);
app.controller('TestController',['$scope', '$http', function ($scope, $http) {
    //Get data from server
    $http.get("server.php")
        .then(function(response) {
            //First function handles success
            $scope.content = response.data;
        }, function(response) {
            //Second function handles error
            $scope.content = "Something went wrong";
    });
});

Server.php will communicate with the database and send to Angular (client).

//$data will be from database
return json_encode($data);

AngularJS is a JavaScript Framework (Client)

  • It can be added to an HTML page with a <script> tag.
  • It is a library written in JavaScript.

MongoDB (Database Server)

  • MongoDB is an open source database that uses a document-oriented data model.

Now you need a server to communicate / create a web application. Nowadays, PHP / NodeJs can be used to create a web application.

AngularJs + PHP

Few of the references

CRUD example with AngularJs+PHP+MongoDB

AngularJs example with PHP

10 AngularJs CRUD examples

AngularJs + NodeJs + MongoDb

Todo App with NodeJs + AngularJs

8 Comments

Thanks for your positive response....But can you please explain the code for "content.php" that you have included.
@JunedLaliwala : No problem, I've already few of the examples in my answer for your reference.
See I have created one Collection in MongoDB....Inside that collection "mycol" (name of collection)....I have defined some values......when I am trying to access that collection values......I am getting the message in the browser as :"It looks like you are trying to access MongoDB over HTTP on the native driver port.".........my url is : "localhost:27017/myDB/mycol"
@JunedLaliwala Nobody can see, what you're localhost doing :) That your local system only. If possible put your full code or visit links I've added in my answer that will definitely helps you to do it.
this is my server.js file...................................................................var mongoose = require('mongoose'); var db = mongoose.connection; db.on('error', console.error); db.once('open', function() { mongoose.connect('mongodb://localhost/myDB');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.