[英]How can I use dependency injection in Angular 1 with controllers and services in different namespaces?
I am trying to introduce a clean folders structure for the scripts in my project using Angular 1 and TypeScript 2. I want to have one file app.ts where I will declare my app and controllers, services etc. Then in separate folders like Controllers I would like to keep my controllers in separate files. 我试图为使用Angular 1和TypeScript 2的项目中的脚本引入一个干净的文件夹结构。我想要一个文件app.ts,在其中我将声明我的应用程序和控制器,服务等。然后在单独的文件夹中,例如Controllers希望将我的控制器保存在单独的文件中。
What I have now does not work properly when I am trying to use namespaces and separate classes. 当我尝试使用名称空间和单独的类时,我现在无法正常使用。 It works fine if I put controllers declaration in a separate file completely using constructor inside instead of the namespaces as in the example below.
如果我将控制器声明完全放在内部使用构造函数而不是下面的示例中的名称空间,则可以很好地工作。 When I run the application I can see two javascript errors:
当我运行该应用程序时,我可以看到两个javascript错误:
-Uncaught ReferenceError: TypeScript is not defined, at app.ts:6 (in this case TypeScript is my namespace) -未捕获的ReferenceError:在app.ts:6处未定义TypeScript(在这种情况下,TypeScript是我的命名空间)
-Error: [ng:areq] Argument 'BrowseMovieTicketsController' is not a function, got undefined http://errors.angularjs.org/1.4.10/ng/areq?p0=BrowseMovieTicketsController&p1=not%20a%20function%2C%20got%20undefined -错误:[ng:areq]参数'BrowseMovieTicketsController'不是一个函数,未定义http://errors.angularjs.org/1.4.10/ng/areq?p0=BrowseMovieTicketsController&p1=not%20a%20function%2C%20got %20undefined
In section of my html file the scripts are included in this ordering: 在我的html文件的部分中,脚本包含在此顺序中:
<script src="/bower_components/angular/angular.js"></script>
<script src="/Scripts/TypeScript/app.js"></script>
<script src="/Scripts/TypeScript/Services/MovieInformationService.js"></script>
<script src="/Scripts/TypeScript/Controllers/BrowseMovieTicketsController.js"></script>
Body of index.html index.html的主体
<div ng-app="myApp" ng-controller="BrowseMovieTicketsController">
<a ng-click="GetMovie()">Test</div></div>
File app.ts: 文件app.ts:
/// <reference path="./../Scripts/typings/angularjs/angular.d.ts"/>
var myApp = angular.module('myApp', []);
//Controllers
myApp.controller("BrowseMovieTicketsController", TypeScript.Controllers.BrowseMovieTicketsController);
//Services
myApp.service("MovieInformationService", TypeScript.Services.MovieInformationService);
File BrowseMovieTicketsController.ts: 文件BrowseMovieTicketsController.ts:
/// <reference path="./../../Scripts/typings/angularjs/angular.d.ts"/>
namespace TypeScript.Controllers {
export class BrowseMovieTicketsController {
scope: any;
movieInformationService: Services.MovieInformationService;
static $inject = ["$scope", "MovieInformationService"];
constructor($scope: any, MovieInformationService: Services.MovieInformationService) {
this.scope = $scope;
this.movieInformationService = MovieInformationService;
}
}
}
File MovieInformationService.ts: 文件MovieInformationService.ts:
namespace TypeScript.Services {
"use strict";
export class MovieInformationService {
public GetMovie() {
console.log("hello");
}
}
}
You need to declare your controller in your module. 您需要在模块中声明控制器。 Try this:
尝试这个:
namespace TypeScript.Controllers {
export class BrowseMovieTicketsController {
static $inject = ["$scope", "MovieInformationService"];
constructor(private $scope: any, private MovieInformationService: Services.MovieInformationService) {
}
}
myApp.controller("BrowseMovieTicketsController", TypeScript.Controllers.BrowseMovieTicketsController);
}
Same for declare your service 声明服务也一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.