使用打字稿从回调中获取控制器引用
Get controller reference from callback using typescript
我有以下简单的控制器,它使用指令/组件并将函数作为绑定传递。
当调用函数时,我没有引用使用任何控制器类服务。
在这种情况下,在我的控制器public onTileClicked
功能中,我无法访问tileService
控制器 js:
namespace app.dashboard {
'use strict';
export class DashboardController {
static $inject:Array<string> = ['$q', 'logger', 'tileService'];
constructor(private $q:ng.IQService,
private tileService:TileService) {
}
public tiles:Array<ITile> = [];
public onTileClicked(tile:ITile) {
this.tileService.getTiles(tile.ID) // No access to tileService
.then((data)=> {
this.tiles = data; // Won't have access to this.tiles
})
}
}
angular
.module('app.dashboard')
.controller('DashboardController', DashboardController);
}
控制器 html:
<div class="tiles-container">
<tile-component ng-repeat="tile in DashboardCtrl.tiles" tile="tile"
on-tile-clicked="DashboardCtrl.onTileClicked">
</tile-component>
</div>
指令 js:
class TileComponent {
tile:ITile;
onTileClicked:Function;
/* @ngInject */
constructor() {
}
tileClicked() {
this.onTileClicked()(this.tile);
}
}
angular.module('app.dashboard')
.component('tileComponent', {
templateUrl: 'app/dashboard/directives/tile.html',
controller: TileComponent,
controllerAs: 'tileCtrl',
bindings: {
tile: '<',
onTileClicked: "&"
}
});
onTileClicked js:
DashboardController.prototype.onTileClicked = function (tile) {
var _this = this;
this.tileService.getTiles(tile.ID)
.then(function (tiles) {
_this.tiles = tiles;
});
};
你对函数的绑定(在 html 中)是错误的。您错过了括号:
<tile-component ng-repeat="tile in DashboardCtrl.tiles" tile="tile"
on-tile-clicked="DashboardCtrl.onTileClicked()"> <!-- HERE -->
</tile-component>
相关文章:
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- Ui路由器:获取控制器中视图的名称
- CodeIgniter:用于获取包括图像在内的值的控制器
- 使用ajax在同一页面中获取控制器动作渲染
- 在控制器中按ID获取XML ViewElement(MultiInput)
- 我正在使用角度 ui-select 进行下拉.如何在控制器中获取所选值
- 从填充了 AJAX 的下拉列表中获取控制器的操作方法中的值
- 从 javascript 获取 MVC 控制器中的操作参数时出错
- 在 AngularJS 中按路径位置获取控制器
- 使用打字稿从回调中获取控制器引用
- 从视图、代码点火器获取控制器中传递的值
- Angularjs.获取控制器的范围.未知的提供程序错误
- 正在获取控制器的null值's使用jquery从javascript传递的操作
- 正在AngularJS中从$parent获取控制器名称
- 获取控制器中$scope的指令值
- 从手动$compile调用中获取控制器
- 在单元测试中获取控制器的编译模板
- 正在获取控制器内部的视图名称
- 如何获取控制器中用户选择的单选按钮的值