Recommend to use angular-cli to generate component and service, so we can get testing templates. ng g s heros // generate a heros service Component with injected service: import { TestBed, async, inject } from '@angular/core/testing'; import { AppCom…
When you use ngrx/store and you want to fire a service request. When it sucessfully return the response, you need to dispatch action to tell the store update. So one pattern can be considered to follow is: import {Http, Headers} from '@angular/http';…
一个简单的例子,控制层:.controller('publishController',['$scope','publishService', function($scope,publishService){ publishService.getData().then( function(answer){ $scope.data = answer.data; console.info(answer); }, function(error){ $scope.error = error; } );…
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body ng-app="myApp"> <div ng-controller="firstController"> <ul>…
  早上开车上班, 发现车快没油了, 于是拐进加油站. 有一辆出租车也在加油..   Angular service在一个应用里是以单例形式存在的. 这个单例的实例是由service factory(service工厂)创建的.   加油, 就好比是一个 Angular service; 不论是我加的油, 还是出租车加的油, 都是从这个加油站同一个地下油罐取出来的同样的汽油. 大家使用的都是同一个汽油实例(单例); 我的车与出租车共享了加的汽油的属性, 比如: 都是93#汽油, 掺水的比例, .…
angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): 用于声明不会被修改的值. 变量(Value): 用于声明会被修改的值. 服务(Service): 这个名称跟服务这个大概念同名,就种行为就像是给自己孩子取名为"孩子".只需要创建这个服务,然后等angular把它new出来,保存这个服务,然后就可以到处注入了. 工厂(Factory): …
A @Directive is used to add behavior to elements and components in your application. This makes @Directives ideal for behaviors such as "tracking" which don't belong in a Component, but do belong as a behavior in your application. import {Direct…
You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app. We want to create a service and a componen…
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建的服务 ④在services/http.service.ts中封装一个简单的http请求 import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http…