我对这个 fiddle 有几个问题:
这是这个 Fiddle 的 Javascript 代码,我有一些疑问:
function Main() {
this.friends = [{
name: 'John',
phone: '555-1212',
age: 10,
date: '11 June 2011'},
{
name: 'Mary',
phone: '555-9876',
age: 19,
date: '12 June 2011'},
{
name: 'Mike',
phone: '555-4321',
age: 21,
date: '13 July 2011'},
{
name: 'Adam',
phone: '555-5678',
age: 35,
date: '14 May 2011'},
{
name: 'Julie',
phone: '555-8765',
age: 29,
date: '15 June 2011'}];
}
Main.prototype = {
sort: function(item) {
if (this.predicate == 'date') {
return new Date(item.date);
}
return item[this.predicate];
},
sortBy: function(field) {
if (this.predicate != field) {
this.predicate = field;
this.reverse = false;
} else {
this.reverse = !this.reverse;
}
},
reverse: false
};
1)我注意到 Fiddle 的作者使用函数作为 Controller
而不是使用 app.controller(...)
然后使用 [ Controller ].prototype
。是否建议使用这种方法?我知道我不能问哪种方式更好,因为我有一些关于个人风格的感觉。
2) 在sort
函数中,我看到(关键)词predicate
。这是什么意思?这是从哪里来的?我以前从未见过这个。
3)在这个 fiddle 的 HTML 部分中,我注意到这一行:ng:repeat="friend in Friends.$orderBy(sort,verse)
。这不是那么困难的一行,但我不是熟悉语法$orderBy()
表示法。您总是可以访问该方法吗?该方法来自哪里?
最佳答案
当您使用该模式时它很有用。在进行一些棘手的数据建模或 Controller 继承时,该模式非常有用。不过,大多数时候我个人使用数组 DI 模式。
Minko's Blog Post应该把事情弄清楚。另请注意,在标题“Explicit Dependecy Injection”下,有人插入了这个小注释:“最佳实践:使用上面显示的数组注释。”
PS:我看到一些人在 Controller 的末尾添加了解析函数。然后使用 $routeProvider 在 app.config 中调用该解析函数——(这更干净,但稍微更令人困惑。正如 @Sergey Moiseev 指出的,如果你是一名顾问,那么你可能会更倾向于可读性方面.不过,如果您有一个开发团队,那么你们可以坐下来讨论。
关于javascript - 关于 AngularJS fiddle 的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24246192/