你好,我得到这样的错误是什么原因?
StaticInjectorError [Http]:StaticInjectorError [Http]:NullInjectorError:没有Http提供程序!在tryResolveToken(core.js:1153)的resolveToken(core.js:1211)的NullInjector.get(core.js:923)处在resolveToken(core.js :)的StaticInjector.get(core.js:1024)输入代码1211)在tryResolveToken(core.js:1153)在StaticInjector.get(core.js:1024)在resolveNgModuleDep(core.js:10585)在NgModuleRef.get(core.js:11806)在resolveDep(core.js:12302) )
import { Injectable } from '@angular/core' import {Todo} from './Todo'
import {Http, Response,Headers,RequestOptions} from '@angular/Http' import {Observable} from 'rxjs/Observable' import 'rxjs/add/operator/do' import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map'
@Injectable() export class TodoService{
constructor(private http: Http){}
todoUrl = "https://jsonplaceholder.typicode.com/todos";
getTodos():Observable{ return this.http.get("https://jsonplaceholder.typicode.com/todos") .map((res:Response)=>res.json()) .do(data=>console.log("TODOS LIST")) } }
Run Code Online (Sandbox Code Playgroud) 我为此搜索并尝试了很多东西,我想也许我只是做错了.
我有一个单页面应用程序,它具有非常相似的DOM部分,仅通过从服务提供给它们的数据来区分.每个DOM部分都有不同的Web服务.
我的HTML看起来像这样
<div section="foo">
<ul>
<li ng-repeat="item in collection">{{item.name}}</li>
</ul>
</div>
<div section="bar">
<ul>
<li ng-repeat="item in collection">{{item.cost}}</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的真实例子中,差异更为显着,所以我不能仅仅将"成本"改为"名称"并让它们相同.
我有一个看起来像这样的指令:
angular.module("App").directive("section", ["BaseProvider", function(provider) {
return {
restrict: "A",
scope: {},
link: function($scope, element, attrs) {
provider.query(attrs.section).success(function(response) {
$scope.collection = response; // ** pay attention to this line **
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
所以,它调用BaseProvider的查询方法,传入DOM元素的section属性.所以,在我的例子中,"foo"或"bar".
BaseProvider看起来像这样:
angular.module("App").factory("BaseProvider", ["$http", function($http) {
var urls = {
foo: "/something/foo.v1.json",
bar: "/something/bar.v2.json"
};
return {
query: function(base) {
return $http.get(urls[base]);
}
};
}]); …Run Code Online (Sandbox Code Playgroud) 如果有以下代码但$ route.reload()不刷新页面.
testControllers.controller('LogoutController', [
'$scope', '$http', '$route',
function($scope, $http, $route) {
$scope.logout = function() {
$http.get('/api/authentication/logout').success(function () {
$route.reload();
});
};
}
]);
Run Code Online (Sandbox Code Playgroud) 我有一个角度4应用程序,我试图找到一种最佳实践方法来创建一个解决方案来创建一个服务或某种解决方案来处理许多网址,以便在开发和生产环境中发出http请求.
此时我的解决方案看起来像这样.
import { Injectable } from '@angular/core';
/**
* PathsService is like a properties file for all of the URL paths used in the application.
*/
@Injectable()
export class PathsService {
constructor() {}
productionEnvironment:string = "https://myproductionenvironment.com"
developmentEnvironment:string = "https://mydevelopmentenvironment.com"
activeRoot:string = productionEnvironment;
loginResource = "/loginresources"
employeeResource = "/employeeresouces"
public getLoginResources(): string {
return this.activeRoot+this.loginResource;
}
public getEmployeeResource():string {
return this.activeRoot+this.employeeResource;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我相信这样可以正常工作,但是存在一个小问题,因为我必须在从开发环境切换到生产环境时更改activeRoot.我想知道是否有更好的方法来做到这一点,所以我不必手动切换.我可以使用javascript来获取url,解析它,并以这种方式在生产和开发环境之间切换,但似乎应该有更好的角度方式来解决这个问题.关于这个问题的任何意见将不胜感激.谢谢!