本文介绍了AngularJS。 $资源,MongoLab,宁静的API - 这有什么错我的样品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来AngularJS和爱它,因为我了解它。我试图找出如何与MongoLab使用$资源和RESTful API AngularJS沟通。我有以下两个文件:

I am new to AngularJS and loving it as I learn it. I am trying to figure out how to communicate with MongoLab from AngularJS using $resource and RESTful API. I have the following two files:

index.html:
-----------
<!DOCTYPE html>
<html lang="en">
<head>
    <title>MongoLab Connectivity Test</title>
    <script src="angular.js"></script>
    <script src="angular-resource.js"></script>
    <script src="app3.js"></script>
    <link rel="stylesheet" href="bootstrap.css" />
    <link rel="stylesheet" href="bootstrap-theme.css" />
</head>
<body ng-app="myModule">
    <div ng-controller="display">
      <p>{{data.message}}</p>
    </div>
</body>
</html>

app3.js:
--------
var myModule = angular.module('myModule', ['ngResource']);

myModule.controller('display', function($scope, personService) {
    $scope.data = personService.query();
});

myModule.constant({
            DB_BASEURL: "https://api.mongolab.com/api/1/databases/db1/collections",
            API_KEY: "<MyAPIKey>"
        })

myModule.factory('personService', ['$resource', 'DB_BASEURL', 'API_KEY',
    function($resource, DB_BASEURL, API_KEY)
    {
        return $resource
            (DB_BASEURL+'/persons/:id'
            ,{id: "@id" apiKey: API_KEY}
            );
    }
]);

当我尝试它,我得到以下的输出:

When I try it, I get the following output:

{{data.message}}

{{data.message}}

我不知道我做错了。希望能得到一些帮助。

I am not sure what I am doing wrong. Hoping to get some help.

推荐答案

一个更好的方式来连接是:的

A better way to connect would be : https://github.com/pkozlowski-opensource/angularjs-mongolab

[复制从文档文本,因为它是]

[copying the text from documentation AS IT IS]

首先,你需要包括AngularJS和角mongolab.js脚本:https://raw.githubusercontent.com/pkozlowski-opensource/angularjs-mongolab/master/src/angular-mongolab.js

Firstly you need to include both AngularJS and the angular-mongolab.js script : https://raw.githubusercontent.com/pkozlowski-opensource/angularjs-mongolab/master/src/angular-mongolab.js

然后,你需要配置2个参数:

Then, you need to configure 2 parameters:

MongoLab key (API_KEY)
database name (DB_NAME)

配置参数需要在不断MONGOLAB_CONFIG到指定应用程序的模块:

Configuration parameters needs to be specified in a constant MONGOLAB_CONFIG on an application's module:

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

app.constant('MONGOLAB_CONFIG',{API_KEY:'your key goes here', DB_NAME:'angularjs'});

然后,创建新的资源是非常非常简单,可以归结为调用$ mongolabResource用MongoDB的集合名称:

Then, creating new resources is very, very easy and boils down to calling $mongolabResource with a MongoDB collection name:

app.factory('Project', function ($mongolabResourceHttp) {
    return $mongolabResourceHttp('projects');
});

一旦完成上述的工作你准备好注入,并在您的服务和控制器使用一个新创建的资源:

As soon as the above is done you are ready to inject and use a freshly created resource in your services and controllers:

app.controller('AppController', function ($scope, Project) {
  Project.all().then(function(projects){
    $scope.projects = projects;
  });
});

此外,您还可以在这里检查出的博客更简单的实现:

这篇关于AngularJS。 $资源,MongoLab,宁静的API - 这有什么错我的样品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 19:00