我正在使用AngularJS和TypeScript,现在我需要将视图中的参数传递给控制器。这就是我如何试图做到这一点(通过NG-INIT)的方式:Angular + TypeScript - 从视图传递参数到控制器
<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)
}
我的问题是,我想获得一个类型,但
- 第一种方式,不会被调用
- 第二了。
你能给我一些建议吗?
这个*可能*工作,但这不是'[ng-init']的预期用途(https://docs.angularjs.org/api/ng/directive/ngInit)。 “这个指令可能会被滥用,为你的模板添加不必要的逻辑,只有少数'ngInit'正确的使用,例如为'ngRepeat'的别名特殊属性...'。 'ng-init'是一个指令,具有优先级,这意味着它可能不会按照您期望的顺序执行,并且您可能依赖尚未提供的数据。 – Claies