简体   繁体   English

将变量传递给作为组件的模态

[英]Pass variable to modal which is the component

I would like to pass data to modal, without pass all $scope.我想将数据传递给模态,而不传递所有 $scope。 My modal is in component.我的模态在组件中。

angular.module('app').component('testModal', {
  templateUrl: '/test-modal',
  bindings: {
    close: '&',
    dismiss: '&',
    resolve: '<'
  },


  controller: function ($scope, test) {
    var vm = this;
    this.test = test;

    this.save = () => {
      this.close()
    }

    this.cancel = function () {
      this.dismiss()
    }
  }
})

I try to pass data like below (from another controller)我尝试传递如下数据(来自另一个控制器)

function openModal() {
            testService.getTest().then(function(data) {
                var modalInstance = $uibModal.open({
                    component: 'testModal',
                    size: 'lg',
                    resolve: {
                        test: function () {
                          return data;
                        }
                      }
                  })   
            })

          }

But I have error: angular.js:15567 Error: [$injector:unpr] Unknown provider: testProvider <- test <- catchError但我有错误: angular.js:15567 Error: [$injector:unpr] Unknown provider: testProvider <- test <- catchError

How can I deal with it?我该如何处理? Why "test" isn't readed in my modal component?为什么在我的模态组件中没有读取“测试”?

When you use a component with $uibModal , you don't have to inject the resolved data in the controller. It will be accessible under this.resolve (if you use the resolve binding, as you did).当您使用带有$uibModal的组件时,您不必在 controller 中注入已解析的数据。它可以在this.resolve下访问(如果您使用resolve绑定,就像您所做的那样)。

Change the component's controller definition to this:将组件的 controller 定义更改为:

controller: function ($scope) {      // <-- removed `test` injection
    var vm = this;
    console.log(this.resolve.test);  // <-- `test` is on `resolve` object
                                     // <-- removed this.test = test
    this.save = () => {
      this.close()
    }
    this.cancel = function () {
      this.dismiss()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM