我正在尝试打印使用中的http调用结果Angularrxjs
请考虑以下代码
import { Component, Injectable, OnInit } from '@angular/core';
import { Http, HTTP_PROVIDERS } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
class myHTTPService {
constructor(private http: Http) {}
configEndPoint: string = '/my_url/get_config';
getConfig() {
return this.http
.get(this.configEndPoint)
.map(res => res.json());
}
}
@Component({
selector: 'my-app',
templateUrl: './myTemplate',
providers: [HTTP_PROVIDERS, myHTTPService],
})
export class AppComponent implements OnInit {
constructor(private myService: myHTTPService) { }
ngOnInit() {
console.log(this.myService.getConfig());
}
}
Run Code Online (Sandbox Code Playgroud)
每当我试图打印出getconfig它的结果总是返回
Observable {_isScalar: false, source: Observable, operator: MapOperator} …Run Code Online (Sandbox Code Playgroud) 从官方文档.答ViewChild:
Configures a view query.
View queries are set before the ngAfterViewInit callback is called.
Run Code Online (Sandbox Code Playgroud)
解释是非常小的,我仍然不太明白它用于什么.
从我发现的博客中考虑这个例子.
带走对@ViewChild(TodoInputCmp)内部代码没有影响TodoInputCmp
有人可以给我一些见解吗?
谢谢
我遇到以下代码的一些奇怪的行为.
function linkFunc(scope, element, attribute) {
var page = angular.element($window);
page.bind('scroll', function() {
var windowHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var body = document.body, html = document.documentElement;
var docHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
var windowBottom = windowHeight + window.pageYOffset;
if (windowBottom >= docHeight) {
scope.$apply(attribute.myDirective);
}
});
}
Run Code Online (Sandbox Code Playgroud)
以上是一段代码,用于检测页面底部是否到达,如果到达它将调用任何绑定的函数 myDirective
主要问题是大多数时候延迟加载工作,并myDirective成功调用.但是有些时候延迟加载不起作用,我无法重现这个bug.
我尝试了不同的屏幕大小,不同的浏览器,但似乎这个bug只是随机发生的.
也许有人之前发生过这种情况,可以指出方向吗?
编辑:
更多信息
经过一些实验,我能够重现这个bug.
基本上,当在浏览器中的百分比缩放< 100 %,window.pageY返回一个十进制值,该值是不准确的略微引起windowBottom由处于关闭状态0.1,以0.9
例如.
console.log(windowBottom); // 1646.7747712336175
console.log(docHeight); …Run Code Online (Sandbox Code Playgroud) 我有一些奇怪的问题ng-class,我怀疑它与竞争条件有关.
这是一个有关的例子
这是相关的js代码
self.slideLeft = function() {
if (self.end_index < self.list_of_stuff.length) {
self.direction = 'left';
debugger;
self.start_index = self.start_index + 4;
self.end_index = self.end_index + 4;
self.display_list = self.list_of_stuff.slice(self.start_index, self.end_index);
}
}
self.slideRight = function() {
if (self.start_index > 0) {
self.direction = 'right';
debugger;
self.start_index = self.start_index - 4;
self.end_index = self.end_index - 4;
self.display_list = self.list_of_stuff.slice(self.start_index, self.end_index);
}
}
Run Code Online (Sandbox Code Playgroud)
这是相关的html
<div class="stuff-wrapper">
<div class="stuff"
ng-class="bCtrl.directionClass()"
ng-repeat="stuff in bCtrl.display_list">
{{stuff}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是动画.
.left.ng-enter,
.left.ng-leave { …Run Code Online (Sandbox Code Playgroud) 如果我手动列出复选框
<ul>
<li>
<input type="checkbox" ng-model="checkState"/>
</li>
<li>
<input type="checkbox" ng-model="checkState"/>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
选中一个复选框可选中所有复选框.
但是如果我使用ng-repeat
<div>
<ul>
<li ng-repeat="elem in someArray">
<input type="checkbox" ng-model="checkState" />
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
选中一个复选框只会检查其中一个
是否有一个原因?从DOM看,它们看起来都一样.
这是我的控制器
function test(){
console.log('trace1'); // this gets print on console
myService.useService('someStuff')
.then(function(data){
console.log('trace2'); // this doesn't get print
})
.catch(function(error){
console.log('trace3'); // this doesn't get print
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试套件
describe('test controller promise', function () {
beforeEach(inject(function(
$controller,
$q,
_$rootScope_,
$location,
$anchorScroll,
_myService_,
$window
) {
$rootScope = _$rootScope_;
myService = _myService_;
var testDeferred = $q.defer();
testDeferred.reject('some_error_message');
spyOn(myService, 'useService').and.callFake(function(){
console.log('trace 5'); // this gets called
return testDeferred.promise
});
myController = $controller('myController', {
$rootScope: $rootScope,
myService: myService,
$location: $location,
$anchorScroll: $anchorScroll,
$window …Run Code Online (Sandbox Code Playgroud) 这是我的bootstrap.sh:
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
apt-get install python-pip
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi
Run Code Online (Sandbox Code Playgroud)
这是我的Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
end
Run Code Online (Sandbox Code Playgroud)
我跑了
vagrant reload --provision
vagrant ssh
Run Code Online (Sandbox Code Playgroud)
但是当我输入pip它时返回
程序'pip'目前尚未安装.您可以通过输入以下命令安装它:sudo apt-get install python-pip
我证实我能够
sudo apt-get install library
Run Code Online (Sandbox Code Playgroud)
在ssh终端,所以我不确定是什么问题.
任何帮助,将不胜感激.
考虑一下这个笨蛋
注意:要观察效果,您必须在输入链接后重新运行应用程序
import {Component, OnInit, Input, OnChanges, DoCheck} from 'angular2/core'
@Component({
selector: 'sub',
template: `
<li *ngFor="#elem of sub_list">
<div>{{elem['name']}}</div>
</li>
`
})
export class Sub {
@Input()
sub_list: Object[];
ngOnInit(){
console.log('init');
console.log(this.sub_list);
}
ngOnChanges(){
console.log('change');
console.log(this.sub_list);
}
ngDoCheck() {
console.log('check');
console.log(this.sub_list);
}
}
@Component({
selector: 'my-app',
template: `
<div>
<sub
[sub_list]="my_list"
>
</sub>
</div>
`,
directives: [Sub]
})
export class App {
my_list: Object[] = [];
ngOnInit() {
var vm = this;
setTimeout(function() {
for(var i = 0; …Run Code Online (Sandbox Code Playgroud) 我现在正在研究斯卡拉的变异,我认为我对逆变有很好的理解.例如trait List[-A],我知道这List[Int]是一个超类型List[AnyVal].
但是说我有以下特点:
trait List[+A] {
def cons(hd: A): List[A]
}
Run Code Online (Sandbox Code Playgroud)
为什么cons参数类型错误?
为什么有必要def cons[B >: A](v: B): List[B]?
例如:
val animal_list: List[Animal] = List(tiger, dog)
Run Code Online (Sandbox Code Playgroud)
如果我们打电话:
animal_list.cons(tiger)
Run Code Online (Sandbox Code Playgroud)
既然Tiger <: Animal,不会cons遇到问题?既然B是Tiger,A现在Animal和B >: A不是真的.
如果我有{}以下匹配错误的变量设置
// Somewhere in my angular controller
self = this;
self.myVar = {};
// In my test file
// This errors out. I console.log myVar to make sure that it is indeed {}
expect(myVar).toEqual({});
// Note if I define myVar = {} in the same test spec it works
it('test', function(){
var myVar = {};
expect(myCtrl.myVar).toEqual({});
expect({}).toEqual({});
});
Run Code Online (Sandbox Code Playgroud)
这是什么原因,您将如何绕过这种行为?根据茉莉花文档
it("should work for objects", function() {
var foo = {
a: 12,
b: 34
};
var bar = { …Run Code Online (Sandbox Code Playgroud) javascript ×7
angularjs ×5
angular ×3
typescript ×3
jasmine ×2
bash ×1
checkbox ×1
covariance ×1
css ×1
generics ×1
html ×1
jquery ×1
observable ×1
python ×1
rxjs ×1
scala ×1
vagrant ×1
vagrantfile ×1