【问题标题】:Send value from child controller to parent with $emit使用 $emit 从子控制器向父控制器发送值
【发布时间】:2015-11-02 17:52:31
【问题描述】:

所以我想将下拉列表中的值和输入文本字段从 FirstController 发送到 MainController,并将 datepickers 中的值从 SecondController 发送到 MainControler。我正在尝试使用 $emit 和 $on 但没有成功。也许我以错误的方式从这些输入中获取值...您可以在下面找到我的代码。

主控制器

'use strict';

angular.module('app').controller('MainController', ['$scope',
 function($scope) {
  $scope.$on('send-type-id', getObjectValues);
  $scope.$on('send-date', getDateValues);

  function getObjectValues(e, selectedObjectType, inputObjectId){
   $scope.objectType = selectedObjectType;
   $scope.objectId = inputObjectId;
   console.log($scope.objectType);
   console.log($scope.objectId);
  }

  function getDateValues(e, dateFrom, dateTo){
   $scope.startDate = dateFrom;
   $scope.endDate = dateTo;
   console.log($scope.startDate);
   console.log($scope.endDate);
  } 
 }
]);

第一控制器

    angular.module('app').controller('FirstController',['$scope',
   function($scope){
    $scope.$emit('send-type-id',auditDropdown, selectId);
 }
])

FirstController 视图

  <div ng-controller="SelectionController">
    <div class="well-panel">
        <div class="panel-body">
            <select class="form-control" ng-model="auditDropdown" ng-init="auditDropdown = auditDropdown || config.auditDropdown[0]"
                    ng-options="option.id as option.name for option in config.auditDropdown">
            </select>
            <input class="form-control" ng-model="selectId" type="text">

            <div ng-include="'modules/audit/views/components/audit-datepickers.client.view.html'"></div>

            <div>
                <button class="btn btn-sm btn-default" type="button">
                    {{'audit.navigation.button.generateAuditReport.btn'| translate}}
                </button>
            </div>
        </div>
</div>

选项配置文件

 ...
 "auditDropdown": [
   {"id": "Name1", "name": "Name 1"}, 
   {"id": "Name2", "name": "Name 2"},
   {"id": "Name3", "name": "Name 3"},  
 ]
...

第二控制器

 angular.module('app').controller('SecondController',['$scope',
       function($scope){
        $scope.$emit('send-date',auditDropdown, selectId);
     }
    ])

SecondController 视图

<div ng-controller="SecondController">
    <section id="datepickerSection">
        <div>
            <input
                    type="date"
                    ng-model="startDate"
                    class="form-control"
                    placeholder="mm/dd/yyyy"
                    datepicker-popup
                    />
        </div>
        <div>
            <input
                    type="date"
                    ng-model="endDate"
                    class="form-control"
                    placeholder="mm/dd/yyyy"
                    datepicker-popup
                    />
        </div>
    </section>
</div>

编辑

我收到错误,即在 SecondController 中未定义 auditDropDown....

【问题讨论】:

  • 为什么需要这样做,因为更改 ng-models 会更新父 $scope 中的数据?你不能只使用 ng-change 吗?

标签: javascript html angularjs emit


【解决方案1】:

你应该从$scope中获取auditDropDown,否则js会寻找一个名为auditDropDown的变量(selectId也一样):

$scope.$emit('send-type-id', $scope.auditDropdown, $scope.selectId);
$scope.$emit('send-date',$scope.auditDropdown, $scope.selectId);

【讨论】:

  • 谢谢,我忘记了$scope...顺便问一下,我能问你一些愚蠢的问题吗?好的,我想以函数 getObjectValues() 中的 $scope.objectType 和 $scope.objectId 值为例。然后在其他函数中使用它们,例如 getOjbectValues.objectType + "asdasd" + getOjbectValues.objectID。那么我应该更改函数以返回一些对象还是?谢谢
  • 是的,我会更改这些函数以返回对象,而不是将它们设置为 $scope。
  • 是的,谢谢。我也很困惑为什么默认值不是 ALL,firstController 中的选项标签是 emtpy。我还尝试使用 ng-init="auditDropdown = auditDropdown || config.auditDropdown[0].id" 以及 ng-init="auditDropdown = auditDropdown || config.auditDropdown[0].name" 但没有成功.. .
猜你喜欢
  • 2015-07-12
  • 2018-08-06
  • 2012-10-27
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多