简体   繁体   English

AngularJS 调用嵌入在另一个控制器中的 ng-controller

[英]AngularJS calling an ng-controller embedded inside another controller

Here is a snippet of the cshtml:这是cshtml的一个片段:

<div>
    <button type="button" ng-click="recalcButton()">Recalc Button</button>
</div>
<div ng-controller="appealPartialController">
    <div ng-include="'/Partials/appealsPartial.html'"></div>
</div>

When recalcButton is clicked, I want to reload the data in appealPartialController.单击 recalcButton 时,我想重新加载 AppealPartialController 中的数据。 Any documentation or JS code examples would be greatly appreciated!任何文档或 JS 代码示例将不胜感激!

因此,经过大量的谷歌搜索和一个好主意的仙女,我找到了一个利用angularJS 的内置 $scope 并从父级调用加载函数的解决方案。

$scope.$$childHead.$$nextSibling.loadAppealModel($scope.claimID);

You can use a shared param.您可以使用共享参数。 First of all you create a service like this:首先你创建一个这样的服务:

import { Injectable, EventEmitter } from '@angular/core';
import { Subscription } from 'rxjs/internal/Subscription';

@Injectable({
  providedIn: 'root'
})
export class SharedParamsService {

  invokeFirstComponentFunction = new EventEmitter();
  subsVar: Subscription;

  constructor() { }

  onUpdateEnv(key, val) {
    var toEmit = [key, val]
    this.invokeFirstComponentFunction.emit(toEmit);
  }
}

Now you can emit a variable when clicking your button, so in your component, after importing SharedParamsService and declaring in your constructor private sharedParams: SharedParamsService:现在您可以在单击按钮时发出一个变量,因此在您的组件中,在导入 SharedParamsService 并在您的构造函数中声明私有 sharedParams: SharedParamsService 之后:

recalcButton(){
    this.sharedParams.onUpdateEnv("btn", this.jobObj.name);
}

Finally, in the component you want to reload after importing SharedParamsService and declaring in your constructor private sharedParams: SharedParamsService:最后,在导入 SharedParamsService 并在构造函数中声明私有 sharedParams: SharedParamsService 后要重新加载的组件中:

 this.sharedParams.subsVar = this.sharedParams.
     invokeFirstComponentFunction.subscribe((emitted) => {
        if (emitted[0] == "btn") {
            this.ngOnInit();
        }
      });

of course you can avoid using the key, val way if you have only this button to check, but using this way you can subscribe to multiple buttons toggles or other variables.当然,如果您只有这个按钮要检查,您可以避免使用 key, val 方式,但使用这种方式您可以订阅多个按钮切换或其他变量。

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

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