gpt4 book ai didi

javascript - AngularJS + Typescript => Controller 范围

转载 作者:行者123 更新时间:2023-11-27 23:58:28 30 4
gpt4 key购买 nike

我有一个关于 AngularJS 和 TypeSscript 范围的问题。

我已经进入这个项目了一半,并且正在熟悉 TypeScript。之前我直接用 JavaScript 编写了所有 AngularJS 代码。

此应用程序的 Controller 和 View 中发生了一些异常情况。看起来它们不是将要绑定(bind)到传递给构造函数的 $scope 对象的范围内的成员直接放置在 Controller 类本身上,如下所示:

module Wizard.Controllers {
"use strict";

export class LoginController extends BaseAppealController {
static $inject = ["$q", "$scope"];

private isInForgottenPasswordMode: boolean;
private passwordSent: boolean;
// other properties

private scope;

constructor($q: angular.IQService, $scope) {
super($q);

// other construtor-related activity
this.isInForgottenPasswordMode = false;
this.passwordSent = false;
}

// ..behaviours and views can interact with isInForgottenPasswordMode
// and passwordSent as if they were on a $scope..
}
}

当使用 AngularJS Batarang 查看 View 上的范围时,我可以看到 LoginController 的范围包含这两个属性 - inForgottenPasswordMode 和 passwordSent。

有人可以解释一下这是怎么发生的吗?是不是不需要绑定(bind)到作用域上?这是我在 AngularJS 中错过的东西吗?

最佳答案

在 Angular 1.4 及更高版本中,您可以使用 this 访问 $scope 值。

它用于启用Controller as语法。

你可以看看这个:

http://toddmotto.com/digging-into-angulars-controller-as-syntax/

关于javascript - AngularJS + Typescript => Controller 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050611/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com