Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用Typescript编写AngularJS控制器的最佳实践 我对AngularJS和Web开发很有新意,对C和C++有更多的经验。 因此,我尝试使用Typescript_Javascript_Angularjs_Typescript - Fatal编程技术网

Javascript 用Typescript编写AngularJS控制器的最佳实践 我对AngularJS和Web开发很有新意,对C和C++有更多的经验。 因此,我尝试使用Typescript

Javascript 用Typescript编写AngularJS控制器的最佳实践 我对AngularJS和Web开发很有新意,对C和C++有更多的经验。 因此,我尝试使用Typescript,javascript,angularjs,typescript,Javascript,Angularjs,Typescript,我现在正在一个控制器上工作,出现了一些奇怪的行为。 当使用下面代码中的登录函数时,我们会遇到一个问题,即服务sessionService和userDataService没有在then语句中定义。 然后在测试之前和之后都可以访问它 class UserController { scope: UserControllerScope; location: ng.ILocationService; sessionService: SessionService; userDataServi

我现在正在一个控制器上工作,出现了一些奇怪的行为。 当使用下面代码中的登录函数时,我们会遇到一个问题,即服务sessionService和userDataService没有在then语句中定义。 然后在测试之前和之后都可以访问它

class UserController {
  scope: UserControllerScope;
  location: ng.ILocationService;
  sessionService: SessionService;
  userDataService: UserDataService;

  constructor($scope: UserControllerScope,
              $location: ng.ILocationService,
              userDataService: UserDataService,
              sessionService: SessionService){
    this.scope = $scope;
    this.location = $location;
    this.userDataService = userDataService;
    this.sessionService = sessionService;
  }

  loginUser(username: string, pw: string) {
    var foundUser;
    console.log(this.sessionService);
    console.log(this.userDataService);
    console.log("Got into LoginUser");

    this.userDataService.getUserAsync(username, pw).then(function(foundUserFromDb) {
      console.log(this.sessionService);
      console.log(this.userDataService);
    });

    console.log(this.sessionService);
    console.log(this.userDataService);
    this.location.path("views/projects.html")
  }
}
是否有一种方法需要注入定制服务,以便它们可以在类中的任何地方访问,或者您是否有更好的方法来处理服务和控制器的注入


提前感谢。

then()的上下文中,此
不是控制器。您必须将
捕获到另一个变量中,如
self
。如果您使用箭头函数,TypeScript将为您执行此操作

loginUser = (username: string, pw: string) => {
    var foundUser;
    console.log(this.sessionService);
    console.log(this.userDataService);
    console.log("Got into LoginUser");

    this.userDataService.getUserAsync(username, pw).then(foundUserFromDb => {
        console.log(this.sessionService);
        console.log(this.userDataService);
    });

    console.log(this.sessionService);
    console.log(this.userDataService);
    this.location.path("views/projects.html")
}
编译后的JavaScript应该如下所示

var _this = this;
this.loginUser = function (username, pw) {
    var foundUser;
    console.log(_this.sessionService);
    console.log(_this.userDataService);
    console.log("Got into LoginUser");

    _this.userDataService.getUserAsync(username, pw).then(function (foundUserFromDb) {
        console.log(_this.sessionService);
        console.log(_this.userDataService);
    });

    console.log(_this.sessionService);
    console.log(_this.userDataService);
    _this.location.path("views/projects.html");
};

then()的上下文中,此
不是控制器。您必须将
捕获到另一个变量中,如
self
。如果您使用箭头函数,TypeScript将为您执行此操作

loginUser = (username: string, pw: string) => {
    var foundUser;
    console.log(this.sessionService);
    console.log(this.userDataService);
    console.log("Got into LoginUser");

    this.userDataService.getUserAsync(username, pw).then(foundUserFromDb => {
        console.log(this.sessionService);
        console.log(this.userDataService);
    });

    console.log(this.sessionService);
    console.log(this.userDataService);
    this.location.path("views/projects.html")
}
编译后的JavaScript应该如下所示

var _this = this;
this.loginUser = function (username, pw) {
    var foundUser;
    console.log(_this.sessionService);
    console.log(_this.userDataService);
    console.log("Got into LoginUser");

    _this.userDataService.getUserAsync(username, pw).then(function (foundUserFromDb) {
        console.log(_this.sessionService);
        console.log(_this.userDataService);
    });

    console.log(_this.sessionService);
    console.log(_this.userDataService);
    _this.location.path("views/projects.html");
};

then()的上下文中,此
不是控制器。您必须将
捕获到另一个变量中,如
self
。如果您使用箭头函数,TypeScript将为您执行此操作

loginUser = (username: string, pw: string) => {
    var foundUser;
    console.log(this.sessionService);
    console.log(this.userDataService);
    console.log("Got into LoginUser");

    this.userDataService.getUserAsync(username, pw).then(foundUserFromDb => {
        console.log(this.sessionService);
        console.log(this.userDataService);
    });

    console.log(this.sessionService);
    console.log(this.userDataService);
    this.location.path("views/projects.html")
}
编译后的JavaScript应该如下所示

var _this = this;
this.loginUser = function (username, pw) {
    var foundUser;
    console.log(_this.sessionService);
    console.log(_this.userDataService);
    console.log("Got into LoginUser");

    _this.userDataService.getUserAsync(username, pw).then(function (foundUserFromDb) {
        console.log(_this.sessionService);
        console.log(_this.userDataService);
    });

    console.log(_this.sessionService);
    console.log(_this.userDataService);
    _this.location.path("views/projects.html");
};

then()的上下文中,此
不是控制器。您必须将
捕获到另一个变量中,如
self
。如果您使用箭头函数,TypeScript将为您执行此操作

loginUser = (username: string, pw: string) => {
    var foundUser;
    console.log(this.sessionService);
    console.log(this.userDataService);
    console.log("Got into LoginUser");

    this.userDataService.getUserAsync(username, pw).then(foundUserFromDb => {
        console.log(this.sessionService);
        console.log(this.userDataService);
    });

    console.log(this.sessionService);
    console.log(this.userDataService);
    this.location.path("views/projects.html")
}
编译后的JavaScript应该如下所示

var _this = this;
this.loginUser = function (username, pw) {
    var foundUser;
    console.log(_this.sessionService);
    console.log(_this.userDataService);
    console.log("Got into LoginUser");

    _this.userDataService.getUserAsync(username, pw).then(function (foundUserFromDb) {
        console.log(_this.sessionService);
        console.log(_this.userDataService);
    });

    console.log(_this.sessionService);
    console.log(_this.userDataService);
    _this.location.path("views/projects.html");
};

如何创建控制器?你在缩小你的JavaScript吗?您如何注册您的服务?另外,您可以将构造函数参数更改为
public$scope:UserControllerScope
,而不是说
this.scope=$scope
,但这不是问题的原因。谢谢您的评论。朱棣文的答案可以帮助我解答这个问题。它直接在当时的上下文中使用“this”。您如何创建控制器?你在缩小你的JavaScript吗?您如何注册您的服务?另外,您可以将构造函数参数更改为
public$scope:UserControllerScope
,而不是说
this.scope=$scope
,但这不是问题的原因。谢谢您的评论。朱棣文的答案可以帮助我解答这个问题。它直接在当时的上下文中使用“this”。您如何创建控制器?你在缩小你的JavaScript吗?您如何注册您的服务?另外,您可以将构造函数参数更改为
public$scope:UserControllerScope
,而不是说
this.scope=$scope
,但这不是问题的原因。谢谢您的评论。朱棣文的答案可以帮助我解答这个问题。它直接在当时的上下文中使用“this”。您如何创建控制器?你在缩小你的JavaScript吗?您如何注册您的服务?另外,您可以将构造函数参数更改为
public$scope:UserControllerScope
,而不是说
this.scope=$scope
,但这不是问题的原因。谢谢您的评论。朱棣文的答案可以帮助我解答这个问题。它在当时的上下文中直接使用了“this”。谢谢你的帮助。我必须在当时的环境中监督这种变化。现在它工作得很好。谢谢你的帮助。我必须在当时的环境中监督这种变化。现在它工作得很好。谢谢你的帮助。我必须在当时的环境中监督这种变化。现在它工作得很好。谢谢你的帮助。我必须在当时的环境中监督这种变化。现在它工作得很好。