【发布时间】:2017-03-06 13:37:05
【问题描述】:
这是我的控制器代码,只要函数 init() 运行,它就会为 this.customers 返回 undefined。
myApp.controller('OrdersController',['$routeParams', function ($routeParams) {
var customerId = $routeParams.customerId;
this.orders = null;
this.customers = [
{joined: '2012-12-03', name: 'Amit', city: 'Delhi', orderTotal: 34.53,orders: [{id:1,product:'shoes',total:'34.53'}]},
{joined: '1995-04-24', name: 'Ravi', city: 'Mumbai',orderTotal: 45.34,orders: [{id:2,product:'socks',total:'53.51'}]},
{joined: '2003-03-21', name: 'Flish', city: 'Lonavla', orderTotal: 134.323,orders: [{id:3,product:'books',total:'43.55'}]},
{joined: '2003-10-14', name: 'Meahe', city: 'Pune', orderTotal: 12.33,orders: [{id:4,product:'eraser',total:'12.33'}]}
];
function init() {
console.log('infunc',this.customers);
//search the customers for customerId
for(var i=0; i < this.customers.length; i++){
if(this.customers[i].orders[0].id === parseInt(customerId)) {
this.orders = this.customers[i].orders;
}
}
}
init();
}]);
【问题讨论】:
-
避免使用这个,而是使用 var customer 或 $scope.customer
标签: angularjs angular-controller