如果这个问题听起来太明显,请道歉.
我最近开始探索和学习AngularJS.我经历了一些很好的教程 -
..还有一些我见过的.
我不是说我读过/研究过所有文件.
问题从这里开始 -
现在,提出这个问题,我发现控制器的定义在一个地方是不同的,而在另一些地方它是不同的 -
一个定义使用一种数组符号(不确定正式术语)进行注射:
app.controller("MyCtrl", ['$scope', function($scope){
$scope.someData = "Array notation";
}]);
Run Code Online (Sandbox Code Playgroud)
就是这样,没有阵列:
app.controller("MyCtrl", function($scope){
$scope.someData = "non-array notation";
});
Run Code Online (Sandbox Code Playgroud)
不是说这是我唯一想要理解的东西,但是,我一定很想了解它们之间的区别.
这两者之间有很大的不同吗?
非常感谢.
注意:我确实在SO中搜索了类似的问题,但找不到我要找的东西.抱歉.
我有一个数组(下面的示例数组) -
a = [{"name":"age","value":31},
{"name":"height (inches)","value":62},
{"name":"location","value":"Boston, MA"},
{"name":"gender","value":"male"}];
Run Code Online (Sandbox Code Playgroud)
我想迭代这个对象数组并生成一个新的Object(不是特别减少).
我有这两种方法 -
a = [{"name":"age","value":31},
{"name":"height (inches)","value":62},
{"name":"location","value":"Boston, MA"},
{"name":"gender","value":"male"}];
// using Array.prototype.map()
b = a.map(function(item){
var res = {};
res[item.name] = item.value;
return res;
});
console.log(JSON.stringify(b));
var newObj = [];
// using Array.prototype.forEach()
a.forEach(function(d){
var obj = {};
obj[d.name] = d.value;
newObj.push(obj)
});
console.log(JSON.stringify(newObj))
Run Code Online (Sandbox Code Playgroud)
在这种操作中使用任何一种都不正确吗?另外,我想了解一个用例会比另一个更受欢迎的用例场景吗?或者我应该坚持循环?
我有一个简单的模型-
Ext.define('MyModel', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.field.String'
],
fields: [
{
type: 'string',
name: 'currentA'
},
{
type: 'string',
name: 'currentB'
},
{
name: 'A'
},
{
name: 'B'
}
]
});
Run Code Online (Sandbox Code Playgroud)
这是我正在对该模型的数据执行的一些操作-
onBeforeRender: function(component, eOpts) {
var mystore = Ext.getStore('Mystore');
mystore.load({
callback: function(){
var entity = headerStore.getAt(0);
var bs = entity.data.B;
var as = entity.data.A;
var currentB = entity.data.currentB;
var currentA = entity.data.currentA;
bs.forEach(function(record) {
// do some operations
});
as.forEach(function(record) {
// do some other operations
}); …
Run Code Online (Sandbox Code Playgroud)