GVKun编程网logo

angularjs – 节点http-server不提供更新的html文件(angularjs service)

14

本篇文章给大家谈谈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)

angularjs – 节点http-server不提供更新的html文件(angularjs service)

我正在构建一个带有角度(通常是ui-router)的前端Web应用程序,并通过节点 http-server提供html文件进行本地开发.我注意到当我进行更新时,http-server不提供我的静态HTML文件这对我当地的发展很有挑战性.

我通过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“

angular – “类型’对象’不能分配给”使用新的HttpClient / HttpGetModule“

在 Google’s official Angular 4.3.2 doc here之后,我能够从本地json文件中执行一个简单的get请求.我想练习从JSON占位符站点点击一个真正的端点,但是我在查找放在.subscribe()运算符中的内容时遇到了麻烦.我创建了一个IUser接口来捕获有效负载的字段,但是带有.subscribe(data => {this.users = data})的行会抛出错误类型’Object’不能分配给’IUser []’类型.处理这个问题的正确方法是什么?看起来非常基本,但我是一个菜鸟.

我的代码如下:

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演示

angular4 http RxJS Observable observer

Observable 可观察对象(观察者们),Observer 观察者;

Observable就像快递公司,Observer是快递小哥,那生产者,消费者呢? 他们直接喊快递公司就行啦

首先学会 Http 类的用法

  1. 组件中 引入 Http

// 引入模块
import { Http } from "@angular/http";
// 注入
@Injectable()
export class ApiService {
  constructor(public http: Http){}
  1. 使用方法,通过 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 $ http HTML解析器

总而言之,我们正在使用AngularJS开发一个Web应用程序,并且我们有一个用例/需求(根本不会经常发生),在此我们需要从静态服务器中检索完整的HTML文档。但是,似乎$
http对象返回了原始HTML字符串作为其“数据”。我们试图避免使用外部库(例如jQuery),但是我们需要将该原始HTML字符串解析为可查询的DOM对象。我们可以使用iframe并完成它,但是出于众所周知的原因,我们也试图避免使用iframe。

因此,问题是:AngularJS是否具有HTML解析器(就像JSON一样)?否则,处理这种情况的最优雅方式是什么?

PS:我们曾尝试过筛选Angular的API文档,但老实说,它们是命中注定的,也不是直觉的。

AngularJS controller and $http service

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的相关信息,请在本站查询。

本文标签: