小编Mat*_*s17的帖子

Angular2:将数组转换为Observable

我有一个组件通过http从服务获取数据,问题是每次我显示这个组件时我都不想访问API后端.我希望我的服务检查数据是否在内存中,如果是,则在内存中返回带有数组的observable,如果没有,则发出http请求.

我的组件

import {Component, OnInit } from 'angular2/core';
import {Router} from 'angular2/router';

import {Contact} from './contact';
import {ContactService} from './contact.service';

@Component({
  selector: 'contacts',
  templateUrl: 'app/contacts/contacts.component.html'
})
export class ContactsComponent implements OnInit {

  contacts: Contact[];
  errorMessage: string;

  constructor(
    private router: Router,
    private contactService: ContactService) { }

  ngOnInit() {
    this.getContacts();
  }

  getContacts() {
    this.contactService.getContacts()
                       .subscribe(
                         contacts => this.contacts = contacts,
                         error =>  this.errorMessage = <any>error
                       );
  }
}
Run Code Online (Sandbox Code Playgroud)

我的服务

import {Injectable} from 'angular2/core';
import {Http, Response, Headers, RequestOptions} from 'angular2/http';
import {Contact} …
Run Code Online (Sandbox Code Playgroud)

http rxjs angular

52
推荐指数
4
解决办法
5万
查看次数

为什么我在Laravel中遇到此错误:PHP Catchable致命错误?

我尝试运行时收到此错误php artisan (anything):

PHP Catchable fatal error:  Argument 2 passed to
Illuminate\Routing\UrlGenerator::__construct()
must be an instance of Illuminate\Http\Request, null given, called in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php
on line 56 and defined in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
on line 81

Catchable fatal error: Argument 2 passed to
Illuminate\Routing\UrlGenerator::__construct()
must be an instance of Illuminate\Http\Request, null given, called in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php
on line 56 and defined in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
on line 81
Run Code Online (Sandbox Code Playgroud)

我完全不知道造成它的原因是什么,需要帮助.

提前致谢

php runtime-error laravel laravel-5

11
推荐指数
2
解决办法
7071
查看次数

Laravel:每当我返回模型时,总会返回与之关系

我有2张桌子:

User        |   Doctor
----------  |  ----------
id          |  id
email       |  user_id
name        |  signature
last_name   |  photo
password    |  description
date_birth  |
Run Code Online (Sandbox Code Playgroud)

每个Doctor都与a有关User,但每个User都与a无关Doctor.我是这样做的,因为我不想使用单表继承并最终得到一堆NULL字段.

有没有办法制作,像这样的?

// Instead of
$doctor = Doctor::with('User')->find(1);
$doctor->user->name;
// This
$doctor = Doctor::find(1);
$doctor->name;
Run Code Online (Sandbox Code Playgroud)

PS:不知道在标题中放什么,我应该把它放在哪里以便它与问题更相关?

model laravel eloquent laravel-4

9
推荐指数
2
解决办法
8768
查看次数

Laravel 4表单模型绑定Form :: select

在阅读文档后确定:http://four.laravel.com/docs/html#form-model-binding

我有一个看起来像这样的表单:

{{ Form::model($profile, array('action' => 'ProfilesController@edit', $profile->user_id, 'files' => true)) }}
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), array('class' => 'span12')) }}
{{ From::close() }}
Run Code Online (Sandbox Code Playgroud)

我的问题是:模型绑定不适用于Form :: select,适用于文本输入.我究竟做错了什么??

谢谢你的帮助.

laravel laravel-4

5
推荐指数
2
解决办法
8464
查看次数

使用<select multiple>对Form :: model绑定Laravel

我刚刚解除了Form::model存在的约束力,我很高兴(这太棒了).我每次使用文本,电子邮件,甚至选择它都会尝试它.

我的问题是,它能用于<select multiple>吗?如果是这样,我该如何使用它以及在数据库中保存数组的正确方法是什么?(这可能很糟糕,但我将数组的所有选项与分隔符连接起来并将其保存为文本,我确定这不是正确的方法).

php select model-binding laravel laravel-4

5
推荐指数
1
解决办法
2111
查看次数

PHP array_reduce 初始参数为数组

我有这个初始数组:

[
    0 => ['id' => 5, 'value' => 50],
    1 => ['id' => 6, 'value' => 60],
    2 => ['id' => 7, 'value' => 70],
]
Run Code Online (Sandbox Code Playgroud)

并想将其转换为:

[
    5 => ['value' => 50],
    6 => ['value' => 60],
    7 => ['value' => 70],
]
Run Code Online (Sandbox Code Playgroud)

起初,我尝试使用map,但它无法修改数组键,所以我认为reduce可以解决问题,因为它将数组减少为单个值,在本例中为数组。所以我尝试:

array_reduce(
    $array,
    function($carry, $item) {
        return $carry[$item['id']] = $item['value'];
    },
    []
);
Run Code Online (Sandbox Code Playgroud)

但它返回这个错误Cannot use a scalar value as an array。我究竟做错了什么?无法array_reduce接收数组作为初始值吗?

php arrays reduce dictionary

5
推荐指数
2
解决办法
4534
查看次数

Laravel:属于更改它的功能名称时不工作

我有这种关系 Artist - has many - Album

艺术家班:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Artist extends Model {

  public function albums()
  {
    return $this->hasMany('App\Album');
  }

}
Run Code Online (Sandbox Code Playgroud)

专辑类:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Album extends Model {

  public function artist()
  {
    return $this->belongsTo('App/Artist');
  }

}
Run Code Online (Sandbox Code Playgroud)

如果我这样做:$album->artist完全没问题

但是如果我在Album类中更改函数的名称而不更改模型/类名:

public function artistInfo()
{
  return $this->belongsTo('App\Artist');
}
Run Code Online (Sandbox Code Playgroud)

然后,这不起作用:$album->artistInfo.它回归null给我

PS这不是我真正的架构,但只有当我更改belongsTo的函数名称时才会出现问题.

php relationship laravel eloquent laravel-5

4
推荐指数
1
解决办法
3482
查看次数

Angular2:在EventEmitter中传递单个参数

我有2个组件,一个'创建'组件和一个子'形式'组件.我需要表单组件来传递submit事件以及表单数据.

真正的问题是当我记录收到的事件时,我得到2,而不是一个事件/ arg.这是它记录的内容:Event {isTrusted: true}然后Contact {name: inputName}

如何过滤事件,以便仅在收到Contact对象时才执行操作?

父'创建'组件:

import {Component, OnInit } from 'angular2/core';
import {Contact} from './contact';
import {ContactFormComponent} from './contact-form.component';

@Component({
    selector: 'contact-create',
    template: `
      <h2>Nuevo contacto</h2>

      <div class="panel panel-default">
        <div class="panel-body">

          <contact-form [contact]="contact" (formSubmitted)="saveContact($event)"></contact-form>

        </div>
      </div>
    `,
    directives: [ContactFormComponent]
})
export class ContactCreateComponent {

  contact: Contact = new Contact('');

  constructor(
      private router: Router,
      private contactService: ContactService) { }

  saveContact(args) {
    console.log(args);
  }
}
Run Code Online (Sandbox Code Playgroud)

儿童'形式'组件

import {Component, EventEmitter} from 'angular2/core';
import {NgForm}    from 'angular2/common'; …
Run Code Online (Sandbox Code Playgroud)

eventemitter angular2-forms angular

4
推荐指数
1
解决办法
458
查看次数

如何从传奇中派遣一个thunk?

我知道我不应该试图从传奇中发出雷鸣声,这与redux-saga试图做的事情背道而驰.但是我在一个相当大的应用程序中工作,大部分代码都是用thunk开发的,我们正在逐位迁移,需要从一个传奇内部发送一个thunk.thunk无法更改,因为它被用在其他部分(一个返回一个promise的thunk),所以它会打破很多东西.

configureStore:

const store = createStore(
  rootReducer,
  initialState,
  compose(applyMiddleware(thunk, sagaMiddleware))
);
Run Code Online (Sandbox Code Playgroud)

佐贺:

// Saga (is called from a takeEvery)
function* watchWarehouseChange(action) {
  const companyId = yield select(Auth.id);

  // We use cookies here instead of localStorage so that we persist
  // it even when the user logs out. (localStorage clears on logout)
  yield call(Cookies.set, `warehouse${companyId}`, action.warehouse);

  // I want to dispatch a thunk here
  yield put.resolve(syncItems);
  // put(syncItems) doesn't work either
}
Run Code Online (Sandbox Code Playgroud)

咚:

export function syncItems() {
  console.log('first!');

  return dispatch => …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs redux redux-thunk redux-saga

4
推荐指数
1
解决办法
1356
查看次数