如何在 AngularJS 的页面控制器中使用 Component 中的函数?

How to use a function from Component inside the page controller in AngularJS?

我想在我的 page.controller 中使用组件中的函数 首先不知道可不可以?其次,如果可能的话,我应该怎么做?

这里我在 component.controller 里面有一个叫做 checkStatus 的函数,我想执行它,所以我的 ng-disabled 变成 false 然后我可以点击 SAVE 按钮。

testComponent.component.js:

export default {
  template: temp,
  controller: myComponentController,
  controllerAs: 'vm',
  bindings: {
    popupMsg : '&'
  }
};

function myComponentController() {
  'ngInject';

  this.popupMsg = function() {
    alert("It is working!");
    this.checkStatus();
  };

  this.checkStatus = function() {
    this.mustPass = true;
  };

}

myPage.controller.js:

export class MyPageController {

  constructor() {
    'ngInject'
    ...
  }

  save() {
    ...
  }
}

myPage.html:

<form>
<test-component mandatory="vm.popupMsg()"> </test-component>

<div>
  <button type="submit" ng-click="vm.save()" ng-disabled="!vm.mustPass"> Save </button>
</div>
</form>

Plunker for testing

您的 testComponent 需要 output bindings

testComponent.component.js

bindings: {
  statusChange: '&'

}

function myComponentController() {

     this.checkStatus = function() {
        this.mustPass = true;
        this.statusChange({$event: this.mustPass});
    };

mypage.html

<test-component mandatory="vm.popupMsg()" status-change="vm.onStatusChange($event)"> </test-component>