我正在使用自动完成程序包angular-auto-complete
,控制器示例使用that = this
定义而不是$scope
以看似过时的样式编写。这是当前的工作代码:
app.controller('autocompleteCtrl', autocompleteCtrl);
autocompleteCtrl.$inject = ['$http'];
function autocompleteCtrl($http) {
var that = this;
that.stateName = null;
that.autoCompleteOptions = {
minimumChars: 2,
data: function (term) {
return $http.get('coinMarketList.json')
.then(function (response) {
// ideally filtering should be done on server
term = term.toUpperCase();
var match = _.filter(response.data, function (value) {
return value.longName.toUpperCase().includes(term);
});
return _.pluck(match, 'longName');
});
}
}
}

我试图像这样转换它:
app.controller('autocompleteCtrl', ['$scope', '$http', function($scope, $http) {
$scope.stateName = null;
$scope.autoCompleteOptions = {
minimumChars: 2,
data: function (term) {
return $http.get('coinMarketList.json')
.then(function (response) {
// ideally filtering should be done on server
term = term.toUpperCase();
var match = _.filter(response.data, function (value) {
return value.longName.toUpperCase().includes(term);
});
return _.pluck(match, 'longName');
});
}
}
}])

但是这会破坏代码,控制台也不会给我一个错误。如何正确地将其转换为$scope
变量?