如何从隐藏的输入元素将值传递给角度服务?

时间:2013-08-21 03:34:22

标签: c# asp.net-mvc-4 angularjs

我已经使用angularjs构建了一个服务,但我使用jQuery DOM选择器从隐藏的输入中查找2个值。这有一种气味,并且想知道是否有非jQuery方法来实现这一点。添加$ scope,似乎错了。

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

我应该用ng-model装饰输入吗?

谢谢你, 斯蒂芬

1 个答案:

答案 0 :(得分:1)

ng-model添加到隐藏的输入将无效; Angular不会使用输入值更新模型。不过你可以试试这个:

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

您也可以写custom directive来做到这一点。

相关问题