2016-03-16 131 views
0

我想分配一些標籤創建後。 我有一個Post模型,看起來像這樣:AngularJs綁定模型屬性複選框

var mongoose = require('mongoose'); 

var PostsSchema = { 
    title: String, 
    content: String, 
    postedBy: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'Users' 
    }, 
    comments: [{ 
     text: String, 
     postedBy: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: 'Users' 
     }, 

    }], 

    tags: [String] 

}; 

我想給一些複選框綁定到郵政的「標籤」陣列屬性。

這是我的帖子的路由器看起來像:

///* Create post */ 
postRouter.route('/').post(function (req, res) { 

     mongoose.createConnection('localhost', 'CMS'); 
     console.log(req.body); 

     var post = { 
      title: req.body.title, 
      content: req.body.content, 
      tags: req.body.tags 

     }; 

     if (typeof req.body.title === "undefined" || typeof req.body.content === "undefined") 
     { 
      res.json({message:"Error"}); 
     }else 
     { 
     var newPost = new Posts(post); 
     newPost.save(function (err, post) { 
      if (err) res.json({message:"Error"}); 
      res.json(post); 
     }); 
     } 

}); 

我的控制器看起來像:

$scope.createPost = function(post){ 
     postService.createPost(post); 
     postService.getPosts() 
      .then(modelPosts); 
    } 

而我的看法是這樣的:

div(ng-controller='postController') 
h2 Create Post 
        form 
         div.form-group 
          label(for='title') Title 
           input(type='text', class='form-control', id='title', name='title', placeholder='Title', ng-model='newpost.title', autofocus) 
         div.form-group 
          label(for='password') Content 
           input(type='text', class='form-control', id='content', name='content', placeholder='content', ng-model='newpost.content') 
        div(ng-controller='tagController') 
         h2 Tags 
         div(ng-model='Tags', ng-init='getTags()') 
          ul(ng-repeat='tag in Tags') 
           li 
            label 
             input(ng-model='newpost.tag',value='{{tag.name}}', type='checkbox', name='tag[]') 
             span {{tag.name}} 
        button(ng-click='createPost(newpost)', class='btn btn-small btn-primary') Create Post 

我不知道是什麼將我的視圖綁定到模型的問題。標記被渲染並且複選框被創建,但是當我檢查一個複選框時,它們都被檢查。

回答

0
input(ng-model='newpost.tag', ng-value-true='tag.name', ng-value-false='null' type='checkbox', name='tag[]') 

Docs Input[checkbox]