Stephen Patten
Stephen Patten

Reputation: 6363

How do I pass values to a angular service from a hidden input element?

I have built a service with angularjs, but am using jQuery DOM selector for find 2 values from hidden inputs.. this has a smell to it, and was wondering if there was a non-jQuery way of accomplishing this. Adding $scope, just seems wrong.

app.factory('ignoredPropertiesService', ['$http', function ($http) {
    var sessionId = $('input[name=SessionGuid]').val();
    var contactId = $('input[name=ContactId]').val();

    var ignoredPropertiesService = {};

    ignoredPropertiesService.getIgnoredProperties = function () {
        return $http.get("/Contact/IgnoredProperties?sessionGuid=" + sessionId + "&contactId=" + contactId);
    };

    ignoredPropertiesService.resfreshIgnoredProperties = function () {
        return $http.get('/Contact/RefreshIgnoredProperties?sessionGuid=' + sessionId + '&contactId=' + contactId);
    };

    return ignoredPropertiesService;
}]);

Should I adorn the inputs with ng-model?

Thank you, Stephen

Upvotes: 1

Views: 622

Answers (1)

Michael Benford
Michael Benford

Reputation: 14114

Adding ng-model to a hidden input won't work; Angular won't update the model with the input value. You can try this, though:

<input type="hidden" value="{{ value }}" ng-init="value = 'foobar'">

You can also write a custom directive to do that.

Upvotes: 1

Related Questions