【发布时间】:2015-10-08 00:25:13
【问题描述】:
我正在使用带有 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)
}
我的问题是我想获得一个类型,但是
- 第一种方式永远不会被调用并且
- 第二个也是。
你能给我一些建议吗?
【问题讨论】:
-
这可能有效,但这不是the intended use for
ng-init。 “可以滥用此指令在模板中添加不必要的逻辑量。ngInit只有少数适当的用途,例如为ngRepeat的特殊属性设置别名...”。ng-init是一个指令,具有优先级,这意味着它可能不会按照您期望的顺序执行,并且您可能依赖于尚未提供的数据。
标签: angularjs angularjs-scope typescript