Hi, I need some help transforming a Javascript/jQuery code into AngularJS
directive since I'll use several times on a site and I think this is the
best way to go and avoid DRY. A friend of mine works on this code for a
past project and share this code with me:

app.directive('country', ['$http', function ($http) {
    return {
        restrict: "C",
        link: function (scope, element, attrs) {
            $http.get(Routing.generate('countries')).success(function
(data) {
                if (data.message) {
                    scope.message = data.message;
                } else {
                    scope.countries = data.entities;
                }
            }).error(function (data, status, headers, config) {
                if (status == '500') {
                    $noty.error("There is not connection to the server");
                }
            });
        }
    };
}]);

And maybe this is a starting point, but this is kind different to what I
have in my jQuery code since mine relies directly on Select2 and Growl
components. This is how my code looks like:

var $country= $("select.country"),
        $state = $("input.state"),
        $town = $('input.town'),
        $city = $('input.city');

    $state.select2({
        placeholder: "Select a state",
        ajax: {
            url: function () {
                var country = $country.val();
                return Routing.generate('states', {country_id: country});
            },
            results: function (data) {
                return {
                    results: data.entities
                };
            }
        },
        formatAjaxError: function () {
            return Translator.trans('mensajes.noServerConnection', {},
'AppBundle');
        }
    }).select2("enable", false);

    $country.on("change", function () {
        if ($(this).val() == "23") {
            $('div#location').show();
            $state.select2("enable", true);
        } else {
            $('div#location').hide();
        }
    });

Can any give me some help adapting my code to directive?

Thanks in advance

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to