如何使用angularjs将数据传递到kendo窗口

时间:2018-02-06 21:12:08

标签: javascript angularjs kendo-ui

我已经展示了代码的一部分,因为它是一个很大的控制器和视图。我正试图将带有数据的k-options传递给模态,但是没有设置。

vm.subItemOptions = {};
vm.editPlanSubItem = function() {
// some custom logic to get the data
      var params = {
                planItems: planItems,
                child: child,
                index: index,
                key: key
            };
            
            vm.subItemOptions = params; // trying to pass data to modal unsuccessfully

            vm.editPopup.center();
            vm.editPopup.open();
            
}
 <div ng-controller="MainCtrl as vm">
        <div class="demo-section k-content">
            <button class="k-button"  ng-click="vm.editPlanSubItem()">Edit</button>
        </div>
        
         <div kendo-window="vm.editPopup" k-options="vm.subItemOptions"
             k-width="600" k-height="200" k-visible="false"
             k-content="{ url: siteURL + '/public/scripts/views/edit-sub-item.html'}"
             k-on-open="win2visible = true" k-on-close="win2visible = false"></div>
        </div>

模态视图edit-sub-item.html的一部分是:

<div class="modal-dialog small-dialog" id="modal-edit-sub-planner">
    <div class="modal-content">
        <div class="color-line"></div>
        <div class="modal-header">
            <h3 class="modal-title" ng-bind="message"></h3>
            <div class="row">
                <div class="col-md-12">
                    <label class="item-goal">Goal</label>
                    <div class="plan-items-td">
                        <input ng-disabled="params.planItems[0].Status === 'Approved'" type="text"
                               class="k-textbox plan-items"
                               ng-model="params.planItems[params.index].children[params.key].Goal">

                    </div>
                </div>
            </div>
            //other code
         
        </div>
    </div>
</div>

我怎样才能将params传递给模态?

1 个答案:

答案 0 :(得分:1)

如果您使用的是angularjs 1.5+,我建议使用component (如果你有机会使用像React,Angular和Vue.js这样的基于组件的架构)。无论如何,使用组件非常简单。首先是父组件:

<div ng-controller="MainCtrl as vm">
  <div class="demo-section k-content">
      <button class="k-button"  ng-click="vm.editPlanSubItem()">Edit</button>
  </div>
        
  <div kendo-window="vm.editPopup" k-options="vm.subItemOptions"
     k-width="600" k-height="200" k-visible="false"
     k-on-open="win2visible = true" k-on-close="win2visible = false">
    <edit-sub-item params="vm.subItemOptions"></edit-sub-item>
  </div>
</div>

然后在edit-sub-item组件中:

// Add any injected param in this component like $scope if you need, or any service that you created
function editSubItemController(){
  var vm = this;
  
  // Need to be in one of the component lifecycle hooks
  vm.$onChanges = function(props){
    console.log(vm.params);
  }
}

angular.module('ApplicationName').component("editSubItem", {
    controllerAs: 'vm',
    controller: editSubItemController,
    templateUrl: './public/scripts/views/edit-sub-item.html',
    bindings: {
        params: '<' //one way binding
    }
});
<div class="modal-dialog small-dialog" id="modal-edit-sub-planner">
    <div class="modal-content">
        <div class="color-line"></div>
        <div class="modal-header">
            <h3 class="modal-title" ng-bind="message"></h3>
            <div class="row">
                <div class="col-md-12">
                    <label class="item-goal">Goal</label>
                    <div class="plan-items-td">
                        <input ng-disabled="vm.params.planItems[0].Status === 'Approved'" type="text"
                               class="k-textbox plan-items"
                               ng-model="vm.params.planItems[vm.params.index].children[vm.params.key].Goal">

                    </div>
                </div>
            </div>
            //other code
         
        </div>
    </div>
</div>