为何要在 userListService.userList( newUserName ) 中加参数?
有几个问题想请教下,就是:
myServiceApp.factory('userListService', ['$http',
function($http) {
var doRequest = function( username, path ) {
return $http({
method: 'GET',
url: 'users.json'
});
}
return {
userList: function( username ) {
return doRequest( username, 'userList' );
}
};
}
]);
myServiceApp.controller('ServiceController', ['$scope', '$timeout', 'userListService',
function($scope, $timeout, userListService) {
var timeout;
$scope.$watch('username', function(newUserName) {
console.log(newUserName);
if (newUserName) {
if (timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(function() {
userListService.userList( newUserName )
.success(function(data, status) {
$scope.users = data;
});
}, 350);
}
});
}
]);
代码中凡是斜体并加下划线的参数都可以去掉,(我试过了,可行),老师为何要加这些参数呢,尤其是 userListService.userList( newUserName ) 这个地方,如果要根据newUserName的值来筛选出部分数据显示在前台的话,也不必在这个地方,而是应该在请求成功后的 success 里面根据返回的data值来判断吧??请大神们帮忙看下