javascript - AngularJS 传递 {} 而不是 null

标签 javascript angularjs browser asp.net-web-api

使用 angularjs 1.4.3、IIS、webapi、Chrome 和 IE 10

我有一个问题,我有这样的模型:

{ someKey: null }

此模型是通过 webapi 调用接收的。

在 Angular 中,我将更改提交回服务器上的 webapi 端点,当我从 Chrome 上的网络选项卡通过它时,模型看起来像这样:

{ someKey: {} }

这导致 webapi 服务器出现模型状态问题

似乎 Angular 将 null 更改为空对象,这对我来说似乎是错误的。空对象不是 null。

有这方面的具体知识吗?这是问题还是错误?

提前致谢。

我也在使用 Typescript。

其他详细信息/说明:

我使用 $http 发布到 webapi 端点,如下所示:

var myHttpConfig: ng.IRequestConfig = {
            method: 'POST',
            url: "http://my webapi url here"),
            timeout:  120000,
            data: {},
            headers:  null
        };
myHttpConfig.headers = { 'Content-Type': 'application/json' };
myHttpConfig.data = { someKey: { aField: null } };
localPostPromise = this._$http(myHttpConfig);

WebView 显示此帖子:

{"someKey":{"aField":{} } }

我的控制台日志将其显示为我在配置中设置的数据:

{ \"someKey\":{\"aField\":null } }

首先返回给我的绑定(bind)到模型的原始数据:

{ "someKey":{"aField":null } }

最佳答案

如果我理解你的问题: 使用未定义:

myHttpConfig.data = { someKey: { aField: undefined } };

或者迭代对象属性并执行此操作

for (var property in object) {
   if (object.hasOwnProperty(property) && object[property] == null) {
      delete object[property];
}}

关于javascript - AngularJS 传递 {} 而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696771/

相关文章:

javascript - AngularJS:ContentEditable 问题

Angularjs:ocLazyLoad 与 Requirejs

jquery - 浏览器 View 内容折叠,窗口最小化时?

windows - CoInternet IsFeatureEnabled 在 Delphi 2010 中

javascript - JavaScript 中的沙箱到底是什么?

javascript - AngularJS $http 将原始 HTML 元素放在正文中

angularjs - Angular 用户界面 Bootstrap : not loading templates

Javascript根据点击事件更新值

javascript - 内容安全策略通配符?

javascript - 在浏览器中提交表单和在javascript中多线程