我正在尝试使用 AngularJS 实现客户端身份验证。现在,我将每个路由的访问级别作为工厂注入到我的应用程序的配置函数中。这是我的代码:
shop = angular.module('shop', ['ngCookies']).
provider('AccessLevels',function () {
var accessLevels = {
guest: 7,
customer: 6,
seller: 4
};
return {
$get: function () {
return accessLevels
}
}
}).
config(['$routeProvider', '$locationProvider', 'AccessLevelsProvider', function ($routeProvider, $locationProvider, AccessLevelsProvider) {
var accessLevels = AccessLevelsProvider.$get();
$routeProvider.
when('/index', {templateUrl: "pages/index", controller: IndexController, access: accessLevels.guest}).
when('/user', {templateUrl: "pages/user", controller: UserController, access: accessLevels.customer}).
when('/sign_up', {templateUrl: "pages/sign_up", controller: SignUpController, access: accessLevels.guest});
}]);
Run Code Online (Sandbox Code Playgroud)
是否可以通过 $http.get 请求获取我的访问级别的数据?我无法将 $http 注入到我的提供程序函数中,所以我现在有点无助。任何帮助,将不胜感激。
我是角度js的新手,并试图使用指令.我想在指令的link函数中使用$ http.以下是我的代码
MyApp.directive('appItem', ['$http',function() {
return {
restrict: 'E',
scope: {
transaction: '='
},
templateUrl: 'js/directives/appItem.html' ,
link: function($scope, $http, element, attrs) {
$scope.process = function(trId) {
$http({
method: 'PATCH',
url: 'http://myapp.domain.local/api/v1/items/'+trId+'.json'
}).
then(function successCallback(response) {
console.log(response);
});
}
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
但它给了我错误:
$http is not a function
Run Code Online (Sandbox Code Playgroud) $scope.tempObject = {};
$http({
method: 'GET',
url: '/myRestUrl'
}).then(function successCallback(response) {
$scope.tempObject = response
console.log("Temp Object in successCallback ", $scope.tempObject);
}, function errorCallback(response) {
});
console.log("Temp Object outside $http ", $scope.tempObject);
Run Code Online (Sandbox Code Playgroud)
我得到响应successCallback,但没有得到$scope.tempObject外界$http。它的显示undefined。
如何访问response或$scope.tempObject之后$http
我想问一下这个方法的不同之处我的关注点是.then和.success,函数以及.error感谢之间的区别.
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Run Code Online (Sandbox Code Playgroud)
和
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).success(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}).error( function(data) {
// called …Run Code Online (Sandbox Code Playgroud) 我正在尝试开发一个应用程序,其中Spring Boot用于后端,Angular 2用于前端.
实际上我有连接问题,当我从spring访问mvc登录页面时.
我遇到以下问题:
( 安慰 )
我为FrontEnd端口设置了全局Cors配置.我的请求返回响应状态200.但我无法访问响应,因为我在控制台中收到错误消息.
Spring dosnt有错误.
按照教程,教师正在使用spotify api构建spotify音乐搜索.按照教练所做的每一步,但问题是我的数据永远不会回来,我得到了未定义,没有任何东西出现在屏幕上.但是,如果我直接在浏览器中点击api,则会显示json数据,但不会在我的应用中显示.
其实我已经看过很多像这样的问题,但没有一个能解决我的问题.我认为它可能是一个与网络相关的问题,但是当我设置断点时,我可以在响应中看到结果,但是没有任何东西被加载到视图中,未定义被加载到控制台中
**search.component.html**
<h1>Need Music?</h1>
<p class="lead">
Use the ngSpotify app to browse new releases of your favorite songs. See what your favorite artistes are up to.
</p>
<form>
<div class="form-group">
<input type="text" name="searchStr" [(ngModel)]="searchStr" (keyup.enter)="searchMusic()" class="form-control" placeholder="Search music here..."/>
</div>
</form>
**search.component.ts**
import { Component, OnInit } from '@angular/core';
import { SpotifyService } from '../../Services/spotify.service';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
constructor(private _spotifyservice: SpotifyService) { }
searchStr: string;
searchResult;
searchMusic() …Run Code Online (Sandbox Code Playgroud) 我将许多HTTP请求发送到服务器,并且服务器返回每个请求带有新访问令牌的响应(可能在标题或正文中).
有没有办法在它完成之前或之后处理所有响应,比如某些后端框架中的请求的中间件?
我需要帮助在Angular 4中显示来自api的subscribe输出.我怎么能这样做,因为我写了data.data.data,但它说属性数据不存在于类型对象上.我将如何在浏览器中输出?这是我下面的代码和下面的api图片
import { Component, OnInit } from '@angular/core';
import { NewsService } from '../news.service';
@Component({
selector: 'app-news-list',
templateUrl: './news-list.component.html',
styleUrls: ['./news-list.component.css']
})
export class NewsListComponent implements OnInit {
constructor(private newsService: NewsService) { }
ngOnInit() {
this.newsService.getNews()
.subscribe(
data => {
alert("News Success");
console.log(data);
},
error => {
alert("ERROR");
});
}
}
Run Code Online (Sandbox Code Playgroud) 在HttpClientModule中,是否有一种传递标头和参数来获取请求的方法。
import { HttpHeaders, HttpParams, HttpClient } from @angular/common/http';
const headers = { headers: new HttpHeaders({}) }
let params = new HttpParams({ });
get(url, {params}) // http client get with params
get(url, {headers}); //http client get with headers
Run Code Online (Sandbox Code Playgroud)
我想要类似requestoptions的内容,或者想要一种语法来使httpClient获得发送请求标头和参数。
当前使用搜索参数构建完整的url并发送标头。
http-get angular-http angular-http-interceptors angular angular-httpclient
我正从Http迁移到HttpClient, 我习惯将一些标头添加到我的http请求中,如下所示:
import { RequestOptions, Request, RequestMethod, Headers } from '@angular/http';
this.pass = btoa(cuid + ': ');
this.pass = "Basic " + this.pass;
this.header = new Headers();
this.header.set("Authorization", this.pass);
let options = new RequestOptions({
headers: this.header
});
return this.http.post(myServiceUrl, {}, options)
Run Code Online (Sandbox Code Playgroud)
现在,当迁移到httpClient时,我已经尝试过:
import {HttpClient, HttpHeaders} from '@angular/common/http';
const header = new HttpHeaders();
const pass = 'Basic ' + btoa(cuid + ': ');
header.set('Authorization', pass);
const options = ({
headers: header
});
return this.httpClient.post(myServiceUrl, {}, options); …Run Code Online (Sandbox Code Playgroud) angular-http ×10
angular ×6
angularjs ×4
javascript ×2
api ×1
cors ×1
http-get ×1
json ×1
routes ×1
spring-boot ×1
spring-mvc ×1
typescript ×1