【问题标题】:How to get and post 2 objects from ng-model?如何从 ng-model 获取和发布 2 个对象?
【发布时间】:2019-01-18 16:53:20
【问题描述】:

我这里有一个来自 mongodb 的示例文档,名为 chapters:

{_id:5c014c999cc48c3b0057988b, chapter_name:"AB"}

控制器:这是我获取章节的功能,以及发布到另一个数据库的功能:

.controller('regCtrl', function($http, $location, $timeout, User, Bloodbankchapter) {
    Bloodbankchapter.getBloodbankchapters().then(function(data) {        
        app.bloodbankchapters = data.data.bloodbankchapters;                 
    });

this.regUser = function(regData) {
    User.create(app.regData).then(function(data) {});
};

然后在我的前端注册:

 <form name="regForm" ng-submit="register.regUser(regData); ">
    <label>Chapter Name</label> <!--After I select the chapter name -->
    <select ng-model="register.regData.branch" ng-options ="chapter._id as chapter.chapter_name for chapter in register.bloodbankchapters">
    <option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
    </select>
    
    <label>Chapter ID</label> <!--chapter ID will appear here-->
    <input type="text" ng-model="register.regData.branch_id" hidden>

    <label>Chapter name</label> <!--How to make chapter name appear here?-->
    <input type="text" ng-model="register.regData.branch_name" hidden>
    <button ng-disabled="register.disabled" type="submit">Submit</button>
</form>

我的问题是在我选择章节名称后,第二个输入是正确的,它将 id 发布到数据库中,但是我怎样才能获取章节名称并将其发布到数据库?

我尝试从选择框中获取文本值,然后将其附加到如下文本框:http://jsfiddle.net/kxqJN/,但是当我注册时,正在注册对象 id 而不是这样的章节名称:

`{branch_id:5c014c999cc48c3b0057988b, branch_name:"5c014c999cc48c3b0057988b"}`

如何在第一个框中显示名称,在第二个框中显示 id?

【问题讨论】:

  • 您似乎在此处使用_id 作为您的选项chapter_name 值:chapter._id as chapter.chapter_name。换成chapter.chapter_name for chapter in register.bloodbankchapters track by chapter._id会是什么样子?
  • 当我使用chapter.chapter_name时,它会显示chapter_name,但我需要同时注册id和chapter_name

标签: node.js angularjs mongodb mean-stack


【解决方案1】:

我敢打赌,您还没有尝试过我在 cmets 中的建议:chapter.chapter_name for chapter in register.bloodbankchapters track by chapter._id 将引导您在模型中同时拥有这两个值:

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.register = {
      regData: {
        branch: {},
      },
      bloodbankchapters: [
        {_id:'5c014c999cc48c3b0057988b', chapter_name:"AB"},
        {_id:'5c014c999cc48c3b0057988c', chapter_name:"A"},
      ],
    };
  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
  <select ng-model="register.regData.branch" ng-options ="chapter.chapter_name for chapter in register.bloodbankchapters track by chapter._id">
    <option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
  </select>
  <div>{{register.regData.branch}}</div>
</div>

【讨论】:

  • 现在显示,但这显示了整个文档:这是我得到的{_id":"5c014c999cc48c3b0057988b","chapter_name":"Caloocan","chapter_address":"7th Ave. Grace Park , Caloocan City","chapter_email":"caloocan@redcross.org.ph","chapter_category":"采血单位/血站","chapter_id":"111"}
  • 是否可以只输出chapter.chapter_id和chapter.chapter_name这样的特定字段
  • 不客气!您仍然可以使用管道修改ng-repeat 中的对象(我认为)并只保留您想要的内容,但我不是角度管道专家。作为一种工作方法,我会使用一个函数来获得你想要的东西,比如 getBloodbankchapters() 中的`,但那是因为我不是专家:P
【解决方案2】:

试试这个方法,你会在register.regData.branch

中得到选中的章节对象
<select ng-model="register.regData.branch" ng-options ="chapter as chapter.value for chapter in register.bloodbankchapters">

【讨论】:

  • 不起作用我需要在第二个文本框中显示 id
  • 显示 branch_name 和 branch_id 我认为你需要使用 ng-model="register.regData.branch.branch_id" ng-model="register.regData.branch.branch_name"
  • 可以在一个输入中绑定这个吗?
猜你喜欢
  • 1970-01-01
  • 2019-10-25
  • 2020-06-05
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多