Angular2 Observable共享无效并且重复http调用
BuildingService.ts
@Injectable()
export class BuildingService {
constructor(private http: Http){
}
buildings$: Observable<Building[]>;
this.buildings: Building[];
getData() : Observable<Building[]>{
this.buildings$ = this.http.get('http://localhost:8080/buildings').share().map(this.extractData);
this.buildings$.subscribe(buildings => this.buildings = buildings);
return this.buildings$;
}
private extractData(res: Response) {
let body = res.json();
return body;
}
}
Run Code Online (Sandbox Code Playgroud)
component1.ts
export class component1 {
constructor( private buildingService: BuildingService) {}
this.subscription = this.buildingService.getData()
.subscribe(buildings => console.log(buildings),
error => this.errorMessage = <any>error);
}
Run Code Online (Sandbox Code Playgroud)
component2.ts
export class component2 {
constructor( private buildingService: BuildingService) {}
this.subscription = this.buildingService.getData()
.subscribe(buildings => console.log(buildings), …
Run Code Online (Sandbox Code Playgroud) export interface Element {
name: string;
}
export class Room implements Element {
name: string;
type:string
}
export class Hall implements Element {
name: string;
}
Run Code Online (Sandbox Code Playgroud)
我的varibale类型如下所示
selectedElement: Element;
Run Code Online (Sandbox Code Playgroud)
现在在HTML中我如何检查对象是否具有属性'类型'?
<div *ngIf="selectedElement?.type">
my div
</div>
Run Code Online (Sandbox Code Playgroud) 任何人都可以为我提供一个代码示例来为使用HTTPS保护的控制器编写集成测试吗?使用HTTP我能够写,但使用HTTPS我得到认证错误.
调节器
@RestController
@RequestMapping("/rest/otm/v1")
public class LoginController {
@Autowired
UserAuthenticationService userAuthService;
@ExceptionHandler({ AuthenticationFailedException.class})
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public void login(HttpServletRequest request,HttpServletResponse response) throws AuthenticationDeniedException {
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class IdentityControllerTest {
@Value("${local.server.port}")
int port;
@Test
public void test_Login_Controller() {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("some-proxy", 8080));
clientHttpRequestFactory.setProxy(proxy);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("credential", "pratap");
requestHeaders.set("deviceid", "xyz123");
HttpHeaders …
Run Code Online (Sandbox Code Playgroud) spring-test spring-test-mvc spring-boot spring-restcontroller
控制器:
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', '$filter', function($scope, $filter){
$scope.tickets = [
{title : 'ticket1', assignee : {id:'pratap', name: 'PRATAP'}},
{title : 'ticket2', assignee : null},
{title : 'ticket3', assignee : {id:'ak', name: 'AK'}}
];
$scope.defaultOption = 1;
$scope.options = [
{id:1, name : 'MY TICKETS'},
{id:2, name : 'OTHERS TICKET'}
];
$scope.filteredTicket = [];
$scope.filterData = function(criteria) {
if (criteria == 1) {
$scope.filteredTicket = $filter('filter')($scope.tickets, {assignee: {id:'pratap'}});
} else {
$scope.filteredTicket = $filter('filter')($scope.tickets, {assignee: null, assignee: …
Run Code Online (Sandbox Code Playgroud) MyHighChartComponent.ts
export class MyHighChartComponent {
highchartsConfiguration: any = {
chart: {
events: {
click(e) {
if (!($(event.target)[0].textContent)) {
console.log('clicked'); //this is printing
this.drillDown(); // how to call typescript function here?
}
},
},
}
};
drillDown() {
console.log('drill down method called');
}
}
Run Code Online (Sandbox Code Playgroud)
如何从高图表点击事件内部调用打字稿函数?
我得到以下错误
错误堆栈:TypeError:this.drillDown 不是函数
如何忽略特定URL的CSRF安全性,例如“ / workflow / **”。除了该URL,我还需要所有URL和方法的授权和CSRF安全。
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private PranaUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().requireCsrfProtectionMatcher(new AllExceptUrlStartedWith("/workflow"))
.and().authorizeRequests()
.antMatchers("/rest/**", "/tasklist").authenticated()
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/index.html")
.and().exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
.and().formLogin().successHandler(authenticationSuccessHandler)
.and().formLogin().failureHandler(authenticationFailureHandler)
.and().csrf().csrfTokenRepository(csrfTokenRepository()).and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
}
private static class AllExceptUrlStartedWith implements RequestMatcher {
private static final String[] ALLOWED_METHODS =
new String[] {"GET"};
private final String[] allowedUrls;
public AllExceptUrlStartedWith(String... allowedUrls) {
this.allowedUrls = allowedUrls; …
Run Code Online (Sandbox Code Playgroud) A级
public class ClassA {
private String id;
private Object rawData;
}
Run Code Online (Sandbox Code Playgroud)
B级
public class ClassB {
private String name;
}
Run Code Online (Sandbox Code Playgroud)
C类
public class ClassC {
String address;
}
Run Code Online (Sandbox Code Playgroud)
主班
public class MainExample {
public static void main( String[] args ) throws IOException {
ObjectMapper mapper = new ObjectMapper( );
ClassB classB = new ClassB();
//ClassC classC = new ClassC();
ClassA classA = new ClassA();
classA.setRawData( classB );
//classA.setRawData( classC );
if (classA.getRawData() instanceof ClassB) {
System.out.println("true ");
} else …
Run Code Online (Sandbox Code Playgroud) 视图:
<html ng-app="myApp">
....
<body>
<div ng-controller="myCtrl">
<p>Current user {{loggedOnUser}}</p>
<div ng-if="user.name == {{loggedOnUser}}" ng-repeat="user in users">
<p>Should display</p>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
控制器:
angular.module("myApp", [])
.controller("myCtrl", function($scope){
$scope.loggedOnUser = "xxx"; // for testing purpose
$scope.users = [
{
name : "xxx",
age: "25"
},
{
name : "yyy",
age: "26"
}
];
});
Run Code Online (Sandbox Code Playgroud)
如何使用ng-if与angularJS表达式或有任何其他方法来实现这一点?如果我的loggedOnUser等于user.name,我想显示div
提前致谢
我的Java代码用URL/hello拦截所有调用(返回):
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
Run Code Online (Sandbox Code Playgroud)
我的JavaScript代码调用方法greeting()(Front):
function sendName() {
var name = document.getElementById('name').value;
stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何从Rest客户端调用@MessageMapping("/ hello")方法.我可以通过JavaScript代码连接,甚至可以获得响应并以HTML格式显示它.但我有一个要求,外部系统(通过休息调用)应该调用此@MessageMapping("/ hello"),然后应用程序(后端)应该发送对HTML的响应.我试图从chrome rest客户端插件调用@MessageMapping("/ hello")方法,但是没有成功.
请让我知道如何调用此方法?提前致谢
是否可以在Angular中使用多个数组用于ngFor?
*ngFor="let device of element.devices && element.zones.devices"// something like this
export interface Element {
id: string;
name: string;
zones: Zone[];
devices: Device[];
}
export class Zone {
id: string;
name: string;
devices: Device[];
}
export class Device {
id: string;
name: string;
}
Run Code Online (Sandbox Code Playgroud)
我将有权访问Element对象,因为我需要显示区域内的所有设备和设备.
angular ×4
angularjs ×2
java ×2
rest-client ×2
spring-boot ×2
typescript ×2
charts ×1
csrf ×1
highcharts ×1
jackson ×1
javascript ×1
ngfor ×1
objectmapper ×1
observable ×1
rxjs ×1
rxjs5 ×1
sockjs ×1
spring ×1
spring-test ×1
stomp ×1
this ×1