使用AngularJS和TypeScript以简单的方式引用
Referencing in a simple way using AngularJS and TypeScript
我将VS2015与Gulp一起使用,并尝试使用TypeScript构建AngularJS。
在我的index.html中,我的"app.js"(TS构建的输出和捆绑包)有一个脚本标记。
但是,如果我想引用我的控制器和任何其他服务——我如何才能避免在每个HTML文件中放置相关的脚本标记——有没有一种方法可以直接引用app.js文件并完成它?推荐的方法是什么?
干杯,
如果你想通过script标记在html中只引用一个文件,而其他文件只是在其他js文件中引用,那么你可以使用requirejs。这是一个加载脚本的好工具。在生产中,这是一个很好的方法来连接和缩小所有脚本,以减少请求数量。
我用experimental typescript decorators
、requirejs
和一点想象力解决了这个问题。
所以,在简历中,我写了两个修饰词,一个叫AppStartup
,另一个叫DependsOn
。所以在angular bootstrap中,我可以获取并注册所有依赖项。
我最终得到了类似的东西:
TodoListController.ts
import {DependsOn, AppStartup, ngControllerBase} from "../infra/core";
import {taskManagerDirective} from "../directives/TaskManagerDirective";
@AppStartup({
Name: "TodoSample"
}).Controller({
DependsOn: [taskManagerDirective]
})
export class TodoListController extends ngControllerBase {
public ByControllerAs: string;
constructor() {
super(arguments);
let $httpPromise = this.$get<ng.IHttpService>("$http");
$httpPromise.then(function ($http) {
$http.get('http://www.google.com').success(function (body) {
console.info("google.com downloaded, source code: ", body);
});
});
this.ByControllerAs = "This was included by controllerAs 'this'";
}
}
rowListItemDirective.ts
import {DependsOn, ngDirectiveBase} from "../infra/core";
import {rowListItemDirective} from "./RowListItemDirective";
@DependsOn([rowListItemDirective])
export class taskManagerDirective extends ngDirectiveBase{
public template: string = `
Parent Directive
<table>
<tbody>
<tr row-list-item-directive></tr>
</tbody>
</table>
`;
}
你可以在我的github存储库中看到我在下面做了什么:
https://github.com/klaygomes/angular-typescript-jasmine-seed
相关文章:
- 如何处理javascript中的循环引用,类似于Excel提供迭代限制的方式
- 在没有ajax的情况下将多个变量传递到引用页面的最佳方式
- 在方法中的函数中引用 CoffeeScript 类中的“this”的最佳方式是什么?
- 组内的正则表达式子组以及引用它们的方式
- JavaScript 的这个对象是否以我认为的方式引用新创建的对象
- Javascript:从给定的两个片段中引用“this”的可接受方式
- 有没有快捷方式可以引用我的 DIV 中的元素
- 单例在JS/coffeescript中引用自身的优雅方式
- 如何通过引用传递超时?或者更好的实施方式
- 使用AngularJS和TypeScript以简单的方式引用
- 在对象中引用Javascript对象的正确方式
- 引用导出属性的多种方式之间的差异
- 在UserControl(ascx)中引用javascript的最佳方式
- YUI库-保持对对象的全局引用的最佳方式
- 从另一个属性引用js属性的正确方式
- 将javascript函数的guid作为值而非引用传递的最佳方式
- 以编程方式添加视图,然后引用这些视图
- 在Angular中,在闭包中引用服务属性/方法最合适的方式是什么?
- 通过快捷方式引用Javascript对象
- 为什么我不能以相同的方式引用所有属性