使用ng-model

时间:2016-10-01 15:31:21

标签: angularjs node.js mongodb mongoose ng-tags-input

我现在是编码的新手,所以如果所提供的材料不够清晰,请耐心等待,我会尽我所能。

这是问题

{ 
    email: 'asdf',
    password: 'asdf',
    userlists: { 
        list1: [ [Object], [Object], [Object] ] 
    } 
}

我希望终端将对象数组作为字符串发回给我。

我试图使用这个架构,

user.js的

var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
    email: String,
    password: String,
    userlists: {
        list1: {type:{ sublist1: String }},
        list2: {type:{ sublist2: String }}
    }
});

我尝试将userlists设置为一个对象数组,这会返回相同的不需要的结果,以及 list1 list2的空数组如果他们没有填写。

我希望用户将以下$http.post方法与ng-model一起使用,并且我正在使用ngTagsInput指令让用户在注册时选择预制的“标记”。< / p>

注册-controller.js

 $scope.createUser = function(){
     console.log($scope.newUser);
     $http.post('api/user/signup', $scope.newUser)
     .success(function(response){

     })
     .error(function(error){
         console.log(error);
     })
 };

signup.html

<button ng-click="createUser()">Submit</button>

    <hr>
       <div>
        <tags-input ng-model="newUser.userlists.list1" display-property="sublist1">
          <auto-complete     source="tagsLoadedFromNgTagsInput($query)" 
                            min-length="1" 
                            load-on-focus="true"    
                            load-on-empty="true">
          </auto-complete>
        </tags-input>
       </div>

这是我在执行时从服务器终端获得的结果。 我希望数组中的对象显示为字符串,由上面signup.html中的输入设置。

{ 
    email: 'asdf',
    password: 'asdf',
    userlists: { 
        list1: [ [Object], [Object], [Object] ] 
    } 
}

以下是在mongo shell中记录db.users.find().pretty()时的结果:

{
    "_id" : ObjectId("57efd2dbdcb311107b4830b2"),
    "email" : "asdf",
    "password" : "asdf",
    "userlists" : {
        "list1" : [
            {
                "sublist1" : "sometag1",
                "_id" : "57ed472b0c868aafab696b61"
            },
            {
                "sublist1" : "sometag2",
                "_id" : "57ed472b0c868aafab696b62"
            },
            {
                "sublist1" : "sometag3",
                "_id" : "57ed472b0c868aafab696b63"
            }
        ]
    },
    "__v" : 0
}

有谁知道我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

console.log函数在显示对象之前将其包装起来。为了确保显示所有数据,您必须在调用控制台之前使用JSON.stringify将其转换为字符串。

// Your object
var obj = {some: {nested: {object: {definition: 'hello world!'}}}};

// Print as a single line
console.log(JSON.stringify(obj));

// Print with 2 spaces identation
console.log(JSON.stringify(obj, null, 2));

答案 1 :(得分:0)

使用ng-repeat,因为list1是一个数组。

 <tags-input ng-repeat="list in newUser.userlists.list1">
                <div>{{list.sublist1}}</div>
 </tags-input>
相关问题