在angular.js中获取两个控制器之间没有任何关系的数据



我有两个控制器,它们没有任何关系,当控制器2发生变化时,我想知道控制器1中的smtg。如下所示:在更改controller1之后,更新controller2中的smtg以了解这一点。

export class Test1Controller{
public scope: IScoring;
$onInit() {
}
constructor($scope: IScoring, public $crypto, public toastr: angular.toastr.IToastrService) {          
this.scope = $scope;
this.scope.Ctrl = this;
}

private Understand ()
{
// know aboat Test2Controller happens ...
}
}
export class Test2Controller{
public scope: IScoring;
$onInit() {
}
constructor($scope: IScoring, public $crypto, public toastr: angular.toastr.IToastrService) {          
this.scope = $scope;
}

private Update()
{
this.$http.post('https://api.test.com', this.scope.docs).then((response: any) => {
this.toastr.success("done!");
this.scope.docs.Document = "";
},
function (error) { console.log(error); this.toastr.error("error"); });
}
}

我用$scope解决了这个问题$伊霍尔·亚诺夫奇克在评论中表示,他们在某种程度上是有用的。

export class Test1Controller{
public scope: IScoring;
$onInit() {
}
constructor($scope: IScoring, public $rootScope: any, public $crypto, public toastr: angular.toastr.IToastrService) {          
this.scope = $scope;
this.scope.Ctrl = this;
// understanding about TestController happens ...
$rootScope.$on('mySearchResultsDone', function(event, data) {
vm.results = data;
});
}

private Understand ()
{
// know about Test2Controller happens ...
}
}
export class Test2Controller{
public scope: IScoring;
$onInit() {
}
constructor($scope: IScoring, public $rootScope: any, public $crypto, public toastr: angular.toastr.IToastrService) {          
this.scope = $scope;
}

private Update()
{
this.$http.post('https://api.test.com', this.scope.docs).then((response: any) => {
this.toastr.success("done!");
this.scope.docs.Document = "";
$rootScope.$emit('mySearchResultsDone', {
someData: 'myData'
});
},
function (error) { console.log(error); this.toastr.error("error"); });
}
}

最新更新