Sunday, September 24, 2017

Convert this=that style controller to $scope variables in AngularJS

I am using an autocomplete package angular-auto-complete and the controller example is written in what seems and outdated style with that = this definition instead of $scope. This is the current working code:

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');
                    });
            }
        }
    }

I’m attempting to convert it like this:

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');
                    });
            }
        }
    }])

However this breaks the code, and the console isn’t giving me an errors. How can I properly convert this to $scope variables?

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/25/convert-thisthat-style-controller-to-scope-variables-in-angularjs/
via @lzomedia #developer #freelance #web

No comments:

Post a Comment