GVKun编程网logo

如何从 AngularJS 中的对象获取值?(angularjs获取dom元素)

13

本文将分享如何从AngularJS中的对象获取值?的详细内容,并且还将对angularjs获取dom元素进行详尽解释,此外,我们还将为大家带来关于Angular4.3.3HttpClient:如何从响

本文将分享如何从 AngularJS 中的对象获取值?的详细内容,并且还将对angularjs获取dom元素进行详尽解释,此外,我们还将为大家带来关于Angular 4.3.3 HttpClient:如何从响应的标头获取值?、angular – 如何从表单组中的禁用表单控件中获取值?、Angular.js如何从PHP读取后台数据_AngularJS、angularjs – Angular JS:当对象引用另一个对象时,angular.copy会使浏览器崩溃的相关知识,希望对你有所帮助。

本文目录一览:

如何从 AngularJS 中的对象获取值?(angularjs获取dom元素)

如何从 AngularJS 中的对象获取值?(angularjs获取dom元素)

如何解决如何从 AngularJS 中的对象获取值??

我有一个这样的对象 例子

enter image description here

我需要获取 compressed 对象值。我试过 obj.compressed 但得到 undefiend 值。如何从对象中获取 compressed 值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Angular 4.3.3 HttpClient:如何从响应的标头获取值?

Angular 4.3.3 HttpClient:如何从响应的标头获取值?

(编辑:VS代码;打字稿:2.2.1)

目的是获取请求响应的标头

假设服务中具有HttpClient的POST请求

import {    Injectable} from "@angular/core";import {    HttpClient,    HttpHeaders,} from "@angular/common/http";@Injectable()export class MyHttpClientService {    const url = ''url'';    const body = {        body: ''the body''    };    const headers = ''headers made with HttpHeaders'';    const options = {        headers: headers,        observe: "response", // to display the full response        responseType: "json"    };    return this.http.post(sessionUrl, body, options)        .subscribe(response => {            console.log(response);            return response;        }, err => {            throw err;        });}

第一个问题是我遇到Typescript错误:

''Argument of type ''{     headers: HttpHeaders;     observe: string;     responseType: string;}'' is not assignable to parameter of type''{     headers?: HttpHeaders;    observe?: "body";    params?: HttpParams; reportProgress?: boolean;    respons...''.Types of property ''observe'' are incompatible.Type ''string'' is not assignable to type ''"body"''.''at: ''51,49'' source: ''ts''

确实,当我转到post()方法的ref时,我指出了这个原型(我使用VS代码)

post(url: string, body: any | null, options: {        headers?: HttpHeaders;        observe?: ''body'';        params?: HttpParams;        reportProgress?: boolean;        responseType: ''arraybuffer'';        withCredentials?: boolean;    }): Observable<ArrayBuffer>;

但是我想要这个重载的方法:

post(url: string, body: any | null, options: {    headers?: HttpHeaders;    observe: ''response'';    params?: HttpParams;    reportProgress?: boolean;    responseType?: ''json'';    withCredentials?: boolean;}): Observable<HttpResponse<Object>>;

因此,我尝试使用此结构修复此错误:

  const options = {            headers: headers,            "observe?": "response",            "responseType?": "json",        };

并编译!但是我只是得到json格式的主体请求。

此外,为什么我必须放一个?字段名称末尾的符号?正如我在Typescript网站上看​​到的那样,该符号应该只是告诉用户它是可选的?

我也尝试使用所有字段,不带和带?分数

编辑

this.http.post(url).map(resp => console.log(resp));

Typescript编译器告诉您映射不存在,因为它不是Observable的一部分

我也试过了

import { Response } from "@angular/http";this.http.post(url).post((resp: Response) => resp)

它可以编译,但是得到了不受支持的媒体类型响应。这些解决方案应适用于“ Http”,但不适用于“ HttpClient”。

编辑2

@Supamiu解决方案也得到了不受支持的媒体类型,因此标题中将出现错误。因此,上面的第二个解决方案(具有Response类型)也应该起作用。但从个性上来说,我认为这不是将“Http”与“ HttpClient”混合使用的好方法,因此我将保留Supamiu的解决方案

答案1

小编典典

您可以观察到完整的响应,而不仅仅是内容。为此,您必须传递observe: responseoptions函数调用的参数中。

http  .get<MyJsonData>(''/data.json'', {observe: ''response''})  .subscribe(resp => {    // Here, resp is of type HttpResponse<MyJsonData>.    // You can inspect its headers:    console.log(resp.headers.get(''X-Custom-Header''));    // And access the body directly, which is typed as MyJsonData as requested.    console.log(resp.body.someField);  });

angular – 如何从表单组中的禁用表单控件中获取值?

angular – 如何从表单组中的禁用表单控件中获取值?

我尝试使用表单状态对象初始化我的新FormControl,然后我注意到这个控件不会影响我的表单验证,它也会从FormGroup值中消失.
this.userForm = new FormGroup({
  email: new FormControl('',Validators.required),firstName: new FormControl('',lastName: new FormControl('',role: new FormControl({value: 'MyValues',disabled: true},})

现在,如果我尝试做:

this.userForm.value //email,firstName,lastName

有人遇到过这个问题吗?有解决方案吗
角度版本:5.2.6

这不是问题,是预期的行为.如果您想要包括所有值而不管禁用状态如何,请使用以下命令:
this.userForm.getRawValue()

Angular.js如何从PHP读取后台数据_AngularJS

Angular.js如何从PHP读取后台数据_AngularJS

之前已经有很多方法可以通过angular进行本地数据的读取。以前的例子中,大多数情况都是将数据存放到模块的$scope变量中,或者直接利用ng-init定义初始化的数据。但是这些方法都只为了演示其他功能的效果。这次来学习一下如何将Angular和PHP相结合,从后台读取数据
首先,利用PHP,我们定义了一组后台数据,代码如下(test.php):

<&#63;php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind"); 
$result = $conn->query("SELECT CompanyName, City, Country FROM Customers"); 
$outp = ""; 
while($rs = $result->fetch_array(MYSQLI_ASSOC)) { 
  if ($outp != "") {$outp .= ",";} 
  $outp .= ''{"Name":"'' . $rs["CompanyName"] . ''",''; 
  $outp .= ''"City":"''  . $rs["City"]    . ''",''; 
  $outp .= ''"Country":"''. $rs["Country"]   . ''"}'';  
} 
$outp =''{"records":[''.$outp.'']}''; 
$conn->close(); 
echo($outp); 
&#63;> 
登录后复制

这段代码含义比较简单,连接数据库后,从数据库中利用sql语句选择相应的数据($conn->query("SELECT CompanyName, City,Country FROM Customers"))。之后,利用循环结构,将取出的数据以键值对的形式保存在$outp变量中。
接下来,在js中操作如下:

<div ng-app="myApp" ng-controller="customersCtrl">  
<table> 
 <tr ng-repeat="x in names"> 
  <td>{{ x.Name }}</td> 
  <td>{{ x.Country }}</td> 
 </tr> 
</table> 
</div> 
<script> 
var app = angular.module(''myApp'', []); 
app.controller(''customersCtrl'', function($scope, $http) { 
  $http.get("test.php") 
  .success(function (response) {$scope.names = response.records;}); 
}); 
</script> 
登录后复制

 这里仍然应用了$http服务进行数据的读取,传入数据文件对应的url路径,成功后返回数据,并绑定到$scope.names变量上。

以上就是本文的全部内容,希望对大家的学习有所帮助。

angularjs – Angular JS:当对象引用另一个对象时,angular.copy会使浏览器崩溃

angularjs – Angular JS:当对象引用另一个对象时,angular.copy会使浏览器崩溃

我有以下 JavaScript / Angular代码:
var a = {};
var b = {};
a.ref = b;
b.ref = a;
angular.copy(a);

当angular.copy触发时,浏览器会锁定.我假设这是因为复制功能正在进行深层复制,当它开始复制b的引用时,它进入b然后想要复制它的引用,从而创建一个圆形副本,这将永远不会结束.

这个假设是对的吗?如果是这样,有没有办法避免这种情况?我假设答案将涉及改变数据的外观,但我很想听到别人的想法.

你的假设是对的,问题是循环引用. JSON.stringify也会抱怨这个结构. jQuery.extend在一个非常基本的层次上检测循环引用,并且可以在这里处理你的基本示例,但是 jQuery.extend has its own issues as well.如果你已经在使用jQuery,你可以使用extend,但是否则你可能想看看你自己写的东西,或者你可以使用我通过Google找到的这个花哨的cloneObject函数:

https://gist.github.com/NV/1396086

我们今天的关于如何从 AngularJS 中的对象获取值?angularjs获取dom元素的分享已经告一段落,感谢您的关注,如果您想了解更多关于Angular 4.3.3 HttpClient:如何从响应的标头获取值?、angular – 如何从表单组中的禁用表单控件中获取值?、Angular.js如何从PHP读取后台数据_AngularJS、angularjs – Angular JS:当对象引用另一个对象时,angular.copy会使浏览器崩溃的相关信息,请在本站查询。

本文标签: