Angularjs 将$scope注入typescript控制器
我正在编写我的第一个typescript控制器,在理解如何使用$scope以便能够引用不同代码块中的元素时遇到了一点问题。以下是相关代码:Angularjs 将$scope注入typescript控制器,angularjs,typescript,Angularjs,Typescript,我正在编写我的第一个typescript控制器,在理解如何使用$scope以便能够引用不同代码块中的元素时遇到了一点问题。以下是相关代码: module app.controllers { class TeamsController implements app.domain.Team { //properties teamId: number; teamName: string;
module app.controllers {
class TeamsController implements app.domain.Team {
//properties
teamId: number;
teamName: string;
coach: app.domain.Coach;
division: app.domain.Division;
cheerleaderImage: string;
coachImage: string;
headerImage: string;
logoImage: string;
divisions: app.domain.Division[];
coaches: app.domain.Coach[];
teams: app.domain.Team[];
selectedTeam: app.domain.Team;
teamToBeUpdated: app.domain.Team;
httpService: ng.IHttpService;
static $inject = ['dataAccessService', '$http', '$scope'];
//constructor
constructor(private dataAccessService: app.common.DataAccessService, $http: ng.IHttpService, $scope: ng.IScope) {
this.teams = [];
this.divisions = [];
this.coaches = [];
this.httpService = $http;
var teamResource = dataAccessService.getTeamResource();
var divisionResource = dataAccessService.getDivisionResource();
var coachResource = dataAccessService.getCoachResource();
teamResource.query((data: app.domain.Team[]) => {
this.teams = data;
});
divisionResource.query((data: app.domain.Division[]) => {
this.divisions = data;
});
coachResource.query((data: app.domain.Coach[]) => {
this.coaches = data;
});
this.selectedTeam =
new app.domain.Team(0, "", new app.domain.Coach(0, ""), new app.domain.Division(0, ""), "", "", "", "");
this.teamToBeUpdated =
new app.domain.Team(0, "", new app.domain.Coach(0, ""), new app.domain.Division(0, ""), "", "", "", "");
}
addUpdateTeam(): void {
if (this.teamToBeUpdated.teamId === 0) {
//vm.isBusy = true;
//vm.errorMessage = "";
//post, pass teamToBeUpdated object
this.httpService.post('http://localhost:33201/api/Teams/Add', this.teamToBeUpdated)
.then(function (response) {
//success
this.teams.push(response.data);
this.teams.sort(function (a, b) {
if (a.teamName.toLowerCase() < b.teamName.toLowerCase()) return -1;
if (a.teamName.toLowerCase() > b.teamName.toLowerCase()) return 1;
return 0;
});
this.teamToBeUpdated =
new app.domain.Team(0, "", new app.domain.Coach(0, ""), new app.domain.Division(0, ""), "", "", "", ""); //clear form
}, function (error) {
//failure
//vm.errorMessage = "Failed to save new team: " + error;
})
.finally(function () {
//vm.isBusy = false;
});
}
}
}
//registration with module must be declared after class
angular.module("app")
.controller("teamsController", ['dataAccessService', '$http', '$scope', TeamsController]);
}
模块应用程序控制器{
类TeamController实现app.domain.Team{
//性质
teamId:编号;
团队名称:字符串;
coach:app.domain.coach;
部门:app.domain.division;
啦啦队图像:字符串;
辅助图像:字符串;
头部图像:字符串;
logoImage:字符串;
分部:app.domain.Division[];
Coach:app.domain.Coach[];
团队:app.domain.Team[];
所选团队:app.domain.Team;
teamToBeUpdated:app.domain.Team;
httpService:ng.IHttpService;
静态$inject=['dataAccessService','http','scope'];
//建造师
构造函数(私有dataAccessService:app.common.dataAccessService,$http:ng.IHttpService,$scope:ng.IScope){
这是。团队=[];
这是1.divisions=[];
this.coach=[];
this.httpService=$http;
var teamResource=dataAccessService.getTeamResource();
var divisionResource=dataAccessService.getDivisionResource();
var coachResource=dataAccessService.getCoachResource();
teamResource.query((数据:app.domain.Team[])=>{
这就是团队=数据;
});
divisionResource.query((数据:app.domain.Division[])=>{
此项=数据;
});
coachResource.query((数据:app.domain.Coach[])=>{
this.coach=数据;
});
这是我选择的团队=
新建app.domain.Team(0,”,新建app.domain.Coach(0,”),新建app.domain.Division(0,”,“”,“”,“”,“”,“”,“”);
这是一个更新=
新建app.domain.Team(0,”,新建app.domain.Coach(0,”),新建app.domain.Division(0,”,“”,“”,“”,“”,“”,“”);
}
addUpdateTam():void{
if(this.teamToBeUpdated.teamId==0){
//vm.isBusy=true;
//vm.errorMessage=“”;
//post,传递teamToBeUpdated对象
this.httpService.post('http://localhost:33201/api/Teams/Add,此。团队更新)
.然后(功能(响应){
//成功
这个.teams.push(response.data);
this.teams.sort(函数(a,b){
if(a.teamName.toLowerCase()b.teamName.toLowerCase())返回1;
返回0;
});
这是一个更新=
新建app.domain.Team(0,”,新建app.domain.Coach(0,”),新建app.domain.Division(0,”,,,,,;//清除表单
},函数(错误){
//失败
//vm.errorMessage=“未能保存新团队:”+错误;
})
.最后(函数(){
//vm.isBusy=false;
});
}
}
}
//注册模块必须在下课后声明
角度模块(“应用程序”)
.controller(“teamsController”、“dataAccessService”、“http”、“scope”、“teamsController”);
}
当我在上面使用this
时,我想用$scope
替换它,但是当我尝试这样做时,我得到错误“{propertyname}在类型IScope上不存在”
有人能就如何正确执行此操作提供建议吗?您可以创建一个接口,用所需的属性扩展ng.IScope
interface TeamsScope扩展了ng.IScope{
团队:字符串[]
}
并在构造函数中使用它而不是ng.IScope
构造函数(私有dataAccessService:app.common.dataAccessService,$http:ng.IHttpService,$scope:TeamsScope){
类中定义的所有属性只能通过此访问
例如,您可以在以下类方法之一中访问$scope:
this.$scope
为了在$http服务的处理程序中保持相同的词法作用域,可以使用箭头函数
this.httpService.post('http://localhost:33201/api/Teams/Add', this.teamToBeUpdated)
.then((response) => { this.teams = resonse.data});
您可以在这里找到更多信息。您可以创建一个接口,用所需的属性扩展ng.IScope
interface TeamsScope扩展了ng.IScope{
团队:字符串[]
}
并在构造函数中使用它而不是ng.IScope
构造函数(私有dataAccessService:app.common.dataAccessService,$http:ng.IHttpService,$scope:TeamsScope){
类中定义的所有属性只能通过此访问
例如,您可以在以下类方法之一中访问$scope:
this.$scope
为了在$http服务的处理程序中保持相同的词法作用域,可以使用箭头函数
this.httpService.post('http://localhost:33201/api/Teams/Add', this.teamToBeUpdated)
.then((response) => { this.teams = resonse.data});
您可以在这里找到更多信息。谢谢……这在构造函数中起作用,但是我如何在上面的addUpdateTam方法中访问$scope?关于您的回答,我尝试了,但我想从这个.httpService.post代码块内部访问外部作用域,在这种情况下,如果我是对的,我不能使用它?这是因为这改变了en调用该函数。尝试向then发送箭头函数,而不是普通函数。请给出所指语法的示例,好吗?将其添加到答案中。谢谢