我正在使用带有TypeScript的AngularJS,现在我需要将一个参数从视图传递给控制器。这就是我尝试这样做的方式(通过 ng-init ):
<div class="col-md-9" ng-controller="MapIZSController" ng-init="init('IZS')">
应将 "IZS"
值传递给控制器。控制器看起来像:
export class MapIZSController
{
static $inject = ["$scope", "leafletData"];
private m_scope: IMapIZSScope;
private m_leafletData;
constructor($scope: IMapIZSScope, leafletData)
{
// the first way I tried
$scope.init = function (type) {
console.log("type is: " + type);
};
// the second way I tried
$scope.init = this.init;
}
public init = (init: any) => {
console.log("init is: " + init)
}
我的问题是我想获得一个类型但是
答案 0 :(得分:3)
我会说你走在正确的轨道上。有a working plunker
这可能是一个简化的控制器:
namespace MyNamespace
{
export class MapIZSController
{
static $inject = ["$scope", "leafletData"];
private m_scope: IMapIZSScope;
private m_leafletData;
private _type;
constructor($scope: IMapIZSScope, leafletData)
{
// // the first way I tried
// $scope.init = function (type) {
// console.log("type is: " + type);
// };
// // the second way I tried
// $scope.init = this.init;
}
public init = (type: any) => {
this._type = type
console.log("type is: " + this._type)
}
}
}
这样我们可以调用init it:
<div class="col-md-9"
ng-controller="MapIZSController as ctrl"
ng-init="ctrl.init('IZS')">
</div>
因此,我们使用controllerAs
方法,并且可以通过ctrl.
访问控制器......这样就可以调用ctrl.init()
检查here