小编Geo*_*off的帖子

在 Angular2 中使用无需注入的服务

我是 Angular2 的新手,我有一个类,它在 Angular2 中具有静态函数,我想在其中一个静态函数上实现 http 请求

我有管理表单验证的静态类

FORM VALIDTION
this.userform = this._formbuilder.group({
  first_name: [this.user.first_name, Validators.required],
  last_name: ['', Validators.required],
  username: ['', Validators.compose(
    [
      Validators.required,Validators.minLength(5)
    ]
  )],
  password: ['',Validators.compose([
    Validators.required,ValidationService.passwordValidator
  ])],
  email: ['', Validators.compose([
    Validators.required,ValidationService.emailValidator
  ])],
  role: ['', Validators.required],

})
Run Code Online (Sandbox Code Playgroud)

ValidationService

constructor(private _http:Http){}

 static passwordValidator(control) {
  if(control.value != undefined){
  if (!control.value.match(/^(?=.*[0-9])[a-zA-Z0-9!@#$%^&*]{6,100}$/)) {
    return { 'invalidPassword': true };
  }
 }

 }

static emailValidator(){

return this._http. //this returns an error i would like to 
      //query server to see if email exists

 } …
Run Code Online (Sandbox Code Playgroud)

javascript angular

3
推荐指数
1
解决办法
7284
查看次数

在rxjs函数中返回布尔值失败

我是rxjs的新手,我想返回一个真或假的可观察对象

这是我试过的

checkLoggedin():Observable<boolean> {
  //check from server if user is loggdin
  if(this._tokenService.getToken()){ //this returns synchronous true or false

      this._httpservice.checkifloggedin()
         .subscribe((res)=>{
            return res.data //this comes in as true or false value
              },
              err=>{ return false }
              )    
     }else{

        return false  //this fails with an error of 
                    //type false is not assignable to observable<boolean>
        }

 }
Run Code Online (Sandbox Code Playgroud)

如何更改上面的else部分以使用布尔observable,以便在其他组件中我只能这样做

this._authservice.checkLoggedin()
  .subscribe.....//here get the value whether true or false
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular

3
推荐指数
1
解决办法
3203
查看次数

在 yii2 中获取上传文件的扩展名

我试图在上传过程中获取文件的扩展名,但收到错误消息,路径信息需要字符串:

我努力了:

$path_parts = pathinfo($_FILES['evidence']['name']);
echo $path_parts['extension'];
Run Code Online (Sandbox Code Playgroud)

如何提取文件扩展名,例如jpeg, doc,pdf

php file-upload yii2

2
推荐指数
1
解决办法
8655
查看次数

带有primeng的Angular2复选框失败

我正在使用 angular2 cli,这是我设置表单的方式

在组件中

export class UsersAddComponent implements OnInit {

 ngOnInit() {
    this.userForm = this._formBuilder.group({
      role: ['', [Validators.required]],
      others: this._formBuilder.array([])  //for adding multipe form inputs
    });

   this.addNewUser();

  }

  initAddress() {
     return this._formBuilder.group({     
       sendcred: [''],  //checkbox needs no validation in my logic
        needs_reset: [''], // ''
        .....other fields here
    });
 }


  addNewUser() {  //this is called whenever add new user button is clicked
     const control = <FormArray>this.userForm.controls['others'];
     const addrCtrl = this.initAddress();
     control.push(addrCtrl);
   }
Run Code Online (Sandbox Code Playgroud)

在 html 模板中,我使用了这样的 Primeng 复选框

  <p-checkbox formControlName="needs_reset" label="User …
Run Code Online (Sandbox Code Playgroud)

checkbox typescript angular

2
推荐指数
1
解决办法
5336
查看次数

使用键值将项目推送到typescrip中的数组

我想将一个数组推送到另一个数组,但结果会产生不正确的结果

let pusheditems:any[]  = [];
pusheditems.push(this.yesvalue);
pusheditems.push(this.selectedtruck);
Run Code Online (Sandbox Code Playgroud)

后来当我 console.log(pusheditems)

我得到了一个类型的数组

array(0->yes array, 1->select truck array)
Run Code Online (Sandbox Code Playgroud)

我们正在寻找的是将0的索引值更改为yes,truck等字符串

所以我希望得到

array(yes->yes array, truck->select truck array)
Run Code Online (Sandbox Code Playgroud)

我也试过了

pusheditems.push({yes:this.yesvalue});  //adding yes
pusheditems.push({truck:this.selectedtruck}); //adding truck
Run Code Online (Sandbox Code Playgroud)

但这不起作用

的价值观

this.yesvalues and this.selectedtruck are also arrays
Run Code Online (Sandbox Code Playgroud)

我需要进一步添加什么

arrays typescript angular

2
推荐指数
2
解决办法
1万
查看次数

Angular2向路由器url添加参数

我是angular2的新手,我期待在当前网址没有点击事件上的所述参数时向路由器添加参数

这就是我尝试过的

按钮

<button (click)="onStats()">Proceed to stats </button>
Run Code Online (Sandbox Code Playgroud)

现在的功能

onStats(){
    let url = this.router.url;

    //here i would like to check if the router constains ?stats
    //am stuck

}
Run Code Online (Sandbox Code Playgroud)

所以基本上我有我的网址

locahost:4200/users/roles
localhost:4200/managers
...............
Run Code Online (Sandbox Code Playgroud)

现在我想在url的末尾添加?stats,如果它不存在,那么新的url是

locahost:4200/users/roles?stats
locahost:4200/managers?stats
...........
Run Code Online (Sandbox Code Playgroud)

在我的路由我已经设置

{path:'user/roles:stats' , component:UserRolesStats}
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢

我不想硬编码routerlink ="users/roles?stats",因为其他区域也使用此统计功能

typescript angular

2
推荐指数
1
解决办法
4829
查看次数

在es6中修改数组后,Javascript删除并插入项目

我坚持如何修改我的阵列

我有这样一个数组样本

var data = [
  {id:1, time_in: [{in:12, out:13},{in:122, out:143}]},
  {id:2, time_in: []}  //this has time_in beign empty
]
Run Code Online (Sandbox Code Playgroud)

所以我想修改我的数组,以便在最后,如果数组中的每个项目都有多个time_in它创建为一个新行

所以最后我希望能够实现

var final_array = [
      [
       {id:1, in_time:12, out_time:13},
       {id:1, in_time:122, out_time:143},
       {id:2, in_time:null, out_time:null}
     ]
Run Code Online (Sandbox Code Playgroud)

所以我试过了

data.forEach(item=>{
  const itemindex = data.indexOf(item) //get its index  
  if(item.time_in.length >0){
    data.splice(itemindex , 1) //first remove the item
     //stuck here on how to readd the item to the array and create new rows
       on the previous index


   }else{
     //just declare the intime and …
Run Code Online (Sandbox Code Playgroud)

javascript

2
推荐指数
1
解决办法
63
查看次数

Laravel api 分页指定页面

我在 url 中使用 perPage 和 page 发送我的请求。那是

url = users?page=0&perPage=10 //the get http url
Run Code Online (Sandbox Code Playgroud)

所以我想对我的模型请求进行分页,并指定 page 和 perPage

User::paginate()
Run Code Online (Sandbox Code Playgroud)

所以查看文档我看到我可以添加 perPage 但我看不到如何添加页面。所以从laravel文档我看到你可以添加 perPage 像

User::paginate($request->get('perPage')) //perPage from url request
Run Code Online (Sandbox Code Playgroud)

但是现在我如何将页面参数添加到分页中,以便我可以指定像 0 这样的页面值

我该如何进行?

我也检查了这个问题,但它没有显示如何在雄辩的模型分页中设置它

php laravel

2
推荐指数
1
解决办法
2188
查看次数

Quasar框架q-select在v-model中设置一个对象而不是id

我的选择有以下代码

     <q-select label="Select country"
                behavior="menu"
                :lazy-rules="true"
                :rules="[val => !!val || 'Select your country']"
                dense outlined v-model="form.code" :options="countries"
                option-label="name" option-value="code">
      </q-select>
Run Code Online (Sandbox Code Playgroud)

我有我的 vuejs 代码

<script>
   export default{
      data:()=>({

        form:{
            code:""
         }
        countries:[
              {name:"Country-1", code:"+101"},
              {name:"Country-2", code:"+201"},
           ]
       })

     }

 <script>
Run Code Online (Sandbox Code Playgroud)

从上面我期望的值form.code是代码,例如:+101 但是当我检查时我发现该值是完整的对象。我缺少什么,因为在我的选择中我已经设置了选项标签和选项值我缺少什么?

vuejs2 quasar-framework

2
推荐指数
1
解决办法
4243
查看次数

Angular2 ActivatedRoute参数未定义

我试图在屏幕上显示激活路由参数的值但是失败了

在我的第一个组件

<ul *ngFor="let cate of categories;">
  <li (click)="onviewChecklists(cate.id)">
     <a> {{i+1}} {{cate.category}} </a>
  </li>

</ul>

  onviewChecklists(category:string){
     this._router.navigate(["tsafety/checklist-management/categories/items",
       {category:category}])
 }
Run Code Online (Sandbox Code Playgroud)

现在我的第二个组成部分到了naviangting

category:any;

constructor(
private _router:Router,private activeRoute:ActivatedRoute
 ) {

 }

ngOnInit() {

   this.category = this.activeRoute.snapshot.params['category'];
   console.log(this.category);  //this works
} 
//on this component html {{category}} returns only the first param
Run Code Online (Sandbox Code Playgroud)

在第二个组件html文件中,当{{category}}它onl显示第一个路由的值时

navigate 1 param is checklist display is checklist
navigate 2 param is newcheck  display is checklist
Run Code Online (Sandbox Code Playgroud)

什么可能是错误的,因为console.log()打印正确的值,但{{category}}只打印第一个值

在第二个组件中,我试过了

ngOnInit() {
 this.onGetItems(+this.activeRoute.snapshot.params['category']);

 }

 onGetItems(category){
    return this._checklistitemsService.getAllItems(category)
    .subscribe( …
Run Code Online (Sandbox Code Playgroud)

typescript angular

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