【问题标题】:ng-repeat showing functions names and datang-repeat 显示函数名称和数据
【发布时间】:2013-10-18 03:15:01
【问题描述】:

我是 angularjs 和 mongolab 的初学者..

我有这段代码,可以在 mongolab 中编辑记录:

function EditCtrl($scope, $location, $routeParams, Project) {
    var self = this;
    Project.get({id: $routeParams.projetId}, function(projet) {
        self.original = projet;
        $scope.projet = new Project(self.original);
    });

    $scope.save = function() {
        $scope.projet.update(function() {
            $location.path('/list');
        });
    };
}

完美运行。 我想显示记录中的所有键和值,这是代码:

<div ng-repeat="(key, val) in projet">
    <div>{{key}}:{{val}}</div>
</div>

这就是结果:

_id:{} 破坏: 名称:测试测试 响:4 更新:

在我的记录中,我只有_id、姓名和响铃。我不知道为什么会显示“destroy:”和“update:”!可能是因为我使用此代码连接到 mongolab:

angular.module('mongolab', ['ngResource']).
        factory('Project', function($resource) {
    var Project = $resource('https://api.mongolab.com/api/1/databases' +
            '/_____/collections/_________/:id',
            {apiKey: '___________________'}, {
        update: {method: 'PUT'}
    }
    );

    Project.prototype.update = function(cb) {
        return Project.update({id: this._id.$oid},
        angular.extend({}, this, {_id: undefined}), cb);
    };

    Project.prototype.destroy = function(cb) {
        return Project.remove({id: this._id.$oid}, cb);
    };

    return Project;
});

我应该怎么做才能只显示记录数据?

谢谢

【问题讨论】:

    标签: mongodb angularjs mlab


    【解决方案1】:

    返回一个服务并将你从 get() 收到的项目传回更新和销毁。

    factory('Project', function($resource) {
    return { 
         get: function() {
             return $resource('https://api.mongolab.com/api/1/databases' +
            '/_____/collections/_________/:id',
            {apiKey: '___________________'}, {
             update: {method: 'PUT'}
          },
    
    
    update : function(itm, cb) {
        return item.update({id: item._id.$oid},
        angular.extend({}, item, {_id: undefined}), cb);
    },
    
    destroy : function(item, cb) {
        return item.remove({id: item._id.$oid}, cb);
    };
    

    否则你只能实例化一个并引用它

    factory('Project', function($resource) {
    var item =$resource('https://api.mongolab.com/api/1/databases' +
            '/_____/collections/_________/:id',
            {apiKey: '___________________'}, {
             update: {method: 'PUT'}
    return { 
    
    
    update : function(cb) {
        return item.update({id: item._id.$oid},
        angular.extend({}, item, {_id: undefined}), cb);
    },
    
    destroy : function(cb) {
        return item.remove({id: item._id.$oid}, cb);
    };
    

    【讨论】:

    • 对不起,我是初学者,我只是不明白你的解决方案,我试过了,但出现了一些 js 错误,例如:Uncaught SyntaxError: Unexpected token function for the line update : function(item, cb) {
    • 提供一个 plunkr / jsfiddle 我会尝试修改它。 (你可以模拟 mongo 调用)
    • plnkr.co/edit/vGmB12hrEW0Hdde3zagF?p=preview 点击“编辑器”,你会看到我谈到的问题。非常感谢!
    • plnkr.co/edit/3oLkaerGUQSt86OyWDtl?p=preview 棘手的部分是您的名称“Project”既作为远程域对象又作为 AngularJs 服务。 plnkr 可以工作。
    • 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2016-09-06
    • 2015-08-20
    • 1970-01-01
    • 2016-02-22
    • 2018-11-09
    相关资源
    最近更新 更多