将此=样式控制器转换为AngularJS中的$ scope变量

时间:2017-09-25 05:25:08

标签: angularjs

我正在使用自动完成程序包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变量?

0 个答案:

没有答案