本篇文章给大家谈谈angularjs–节点http-server不提供更新的html文件,以及angularjsservice的知识点,同时本文还将给你拓展angular–“类型’对象’不能分配给”使
本篇文章给大家谈谈angularjs – 节点http-server不提供更新的html文件,以及angularjs service的知识点,同时本文还将给你拓展angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“、angular4 http RxJS Observable observer演示、AngularJS $ http HTML解析器、AngularJS controller and $http service等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- angularjs – 节点http-server不提供更新的html文件(angularjs service)
- angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“
- angular4 http RxJS Observable observer演示
- AngularJS $ http HTML解析器
- AngularJS controller and $http service
angularjs – 节点http-server不提供更新的html文件(angularjs service)
我通过npm install http-server -g全局安装了http-server,并通过转到根项目文件夹并运行http-server来启动它.默认为localhost:8080-两种似乎有效的方法是在每次更新后或通过chrome隐身模式更改端口号.
有没有办法正常使用http-server而无需更改端口或使用隐身模式?
如果它是相关的,我正在使用MBP v.10.11.3
谢谢!
解决方法
the two ways that seem to work is changing the port number after each update or going through chrome incognito mode.
您的问题是客户端缓存.隐身模式有自己的数据目录,与您的正常浏览无关.
幸运的是,http-server提供了一种设置缓存控制头的方法.
-c
Set cache time (in seconds) for cache-control max-age header,e.g. -c10 for 10 seconds (defaults to ‘3600’). To disable caching,use -c-1.
它在此处的文档中列出:https://github.com/indexzero/http-server
您可以在此处阅读HTTP缓存指令:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en
angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“
我的代码如下:
import { Component,OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { IUsers } from './users'; @Component({ selector: 'pm-http',templateUrl: './http.component.html',styleUrls: ['./http.component.css'] }) export class HttpComponent implements OnInit { productUrl = 'https://jsonplaceholder.typicode.com/users'; users: IUsers[]; constructor(private _http: HttpClient) { } ngOnInit(): void { this._http.get(this.productUrl).subscribe(data => {this.users = data}); } }
http.get<IUsers[]>(this.productUrl).subscribe(data => ... // or in the subscribe .subscribe((data: IUsers[]) => ...
此外,我建议在您的模板中使用自动订阅/取消订阅的异步管道,特别是如果您不需要任何奇特的逻辑,并且您只是映射该值.
users: Observable<IUsers[]>; // different type Now this.users = this.http.get<IUsers[]>(this.productUrl); // template: *ngFor="let user of users | async"
angular4 http RxJS Observable observer演示
angular4 http RxJS Observable observer
Observable 可观察对象(观察者们),Observer 观察者;
Observable就像快递公司,Observer是快递小哥,那生产者,消费者呢? 他们直接喊快递公司就行啦
首先学会 Http 类的用法
组件中 引入 Http
// 引入模块 import { Http } from "@angular/http"; // 注入 @Injectable() export class ApiService { constructor(public http: Http){}
使用方法,通过 angular http 返回的就是一个 Observable
this.http //map 操作符返回一个新的 Observable 对象 .map((r,err) => { return r }) //filter 操作符执行过滤操作,然后又返回一个新的 Observable 对象 .filter(r => r.length >= 2) // 抖动时间 .debounceTime(1000) //subscribe 操作符,启动订阅 .subscribe( //订阅 r => { // r 接收订阅成功后返回的数据 },err => { // 错误时的数据 }) // 错误处理 .catch(err=>{})
好吧,这个我知道,怎么自己创建一个呢?
// 创建一个观察者 const myserver = (observer) => { // 返回数据 observer.next('data') // 返回错误 observer.error() // 结束 observer.complete() // 关闭 observer.closed() } // 建立可观察对象 Observable const obs = new Observable(myserver) // 订阅 obs.subscribe(r =>{ console.log(r)})
下面的 myhttp 函数演示了从一个 Get 请求获取 json 并缓存下来的例子,
let mydata myhttp(): Observable<any> { if (mydata) { // 将缓存的数据 mydata 以 Observable 返回 return new Observable((server: Observer<any>) => { server.next(mydata) server.complete() }) } else { // 通过 Angular Http 获取数据 return this.http.get('data.json') .map(r => { // 写入本地 mydata mydata = r return r }) } }
AngularJS $ http HTML解析器
总而言之,我们正在使用AngularJS开发一个Web应用程序,并且我们有一个用例/需求(根本不会经常发生),在此我们需要从静态服务器中检索完整的HTML文档。但是,似乎$
http对象返回了原始HTML字符串作为其“数据”。我们试图避免使用外部库(例如jQuery),但是我们需要将该原始HTML字符串解析为可查询的DOM对象。我们可以使用iframe并完成它,但是出于众所周知的原因,我们也试图避免使用iframe。
因此,问题是:AngularJS是否具有HTML解析器(就像JSON一样)?否则,处理这种情况的最优雅方式是什么?
PS:我们曾尝试过筛选Angular的API文档,但老实说,它们是命中注定的,也不是直觉的。
AngularJS controller and $http service
命令
cd ~/github
ionic start my-reddit blank
cd my-reddit
ionic serve
文件一:
www/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app''s js -->
<script src="js/app.js"></script>
</head>
<body ng-app="myreddit" ng-controller="RedditCtrl">
<ion-pane>
<ion-header-bar>
<h1>My Reddit</h1>
</ion-header-bar>
<ion-content>
<div ng-repeat="story in stories">
<a href="{{story.url}}" target="_blank ">{{story.title}}</a> - {{story.domain}}
</div>
</ion-content>
</ion-pane>
</body>
</html>
文件二:www/js/app.js
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// ''starter'' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of ''requires''
(function () {
var app = angular.module(''myreddit'', [''ionic'']);
app.controller(''RedditCtrl'', function ($http, $scope) {
$scope.stories = [];
$http.get(''https://www.reddit.com/r/Android/.json'')
.success(function (response) {
angular.forEach(response.data.children, function (child) {
$scope.stories.push(child.data);
});
});
});
app.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
})();
我们今天的关于angularjs – 节点http-server不提供更新的html文件和angularjs service的分享已经告一段落,感谢您的关注,如果您想了解更多关于angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“、angular4 http RxJS Observable observer演示、AngularJS $ http HTML解析器、AngularJS controller and $http service的相关信息,请在本站查询。
本文标签: