RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[angularjs]

Martin Hope
alex_t
Asked: 2022-10-07 02:58:25 +0000 UTC

从 AngularJS 中的对象数组中获取信息

  • 0

我有一个实体 Country (fields: id, name, valuta) 和一个实体 City (fields: id, name, country_id) 即 一个国家有很多城市(一对多的关系)。

当我向后端发出请求以获取所有城市时,会出现这样的数组:

[
    {
        "id": 1,
        "name": Moscow
    },
    ...
]

对所有国家的要求:

[
    {
        "id": 1,
        "name": Russia,
        "valuta": "RUB",
        "cities": [
            {
                "id": 1,
                "name": Moscow
            },
            ...
        ]
    },
    ...
]

也就是说,在城市数组中,我看不到该城市属于哪个国家。我想在城市表的页面上显示这些信息:

<div>
        <h3>Список городов</h3>
        <table class="table table-hover" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <td>ID города</td>
                <td>Название</td>
                <td>Страна</td>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="city in Cities">
                <td>{{city.id}}</td>
                <td>{{city.name}}</td>
                <td>?????????_КАК_????????????</td>
            </tr>
            </tbody>
        </table>
    </div>

现在js脚本是这样的:

angular.module('app', []).controller('indexController', function ($scope, $http) {

    const contextPath = 'http://localhost:8082';
    console.log(contextPath);
    
    $scope.getCountries = function () {
        const url = contextPath + '/country';
        $http.get(url)
                .then(function (resp) {
                    $scope.Countries = resp.data;
                });
    };

    $scope.getCities = function () {
        const url = contextPath + '/cities';
        $http.get(url)
                .then(function (resp) {
                    $scope.Cities = resp.data;
                });
    };

    $scope.fillPage = function () {
        $scope.getCountries();
        $scope.getCities();
    };

    $scope.fillPage();
    
});

告诉我怎样才能得到它?我试图以某种方式在 js 脚本中做到这一点。可能需要一些棘手的方法。

angularjs
  • 1 个回答
  • 10 Views
Martin Hope
Christiana Mitchell
Asked: 2021-12-28 19:09:14 +0000 UTC

如何在应用程序加载之前向服务器发出请求

  • 1

有一个Angular应用,在下载整个应用之前需要先请求获取用户数据,也就是需要等待服务器响应,将数据写入ngRx存储,然后才下载应用。

它的用途:应该限制管理页面并检查用户是否被授权,并且此信息应该来自服务器的响应。

还是有其他方法可以实现此功能?

angularjs
  • 1 个回答
  • 10 Views
Martin Hope
midnightelf18
Asked: 2020-02-26 20:41:49 +0000 UTC

在ionic中拥有自己的js

  • 1

有一个项目ionic,最常见的应用程序。而且我缺乏这个框架提供给我的功能,我可以创建一个常规.js文件,在那里编写代码并在项目中使用它,但是即使在我构建项目之后这个脚本也可以工作.apk?

angularjs
  • 1 个回答
  • 10 Views
Martin Hope
JuniorCoder
Asked: 2020-09-14 00:31:41 +0000 UTC

更新数组时出错

  • 1

我有一个组件:

import { Component, OnInit } from '@angular/core';
import { PostService } from '../shared/services/post.service';
import { CreatePost } from '../shared/model/post.model';
import { Router } from '@angular/router';

@Component({
selector: 'app-post',
templateUrl: './post.component.html',
styleUrls: ['./post.component.css']
})
export class PostComponent implements OnInit {

 confirmPost:CreatePost[] = [];
 unconfirmPost:CreatePost[] = [];
searchPost = '';

 constructor(
   private postService: PostService,
  private router: Router
  ) { }

  ngOnInit() {
  this.postService.getPosts()
  .subscribe((post: CreatePost[])=>{
    this.confirmPost = post.filter(p => p.confirm === true).reverse();
    this.unconfirmPost = post.filter(p => p.confirm === false);
  });
}

 watchPost(post:CreatePost){
   this.router.navigate(['/system/post', post.id],{queryParams: post});
 }

 confirmePost(post:CreatePost){
  post.confirm = true;
  this.postService.updatePost(post)
   .subscribe((poste:CreatePost[]) => {
   this.confirmPost = this.confirmPost.unshift(poste);
  });
  }
 }

CreatePost 模型:

export class CreatePost{
  constructor(
    public data: any,
    public category: string,
    public user: string,
    public title: string,
    public text: string,
    public like: number,
    public watch: number,
    public confirm: boolean,
    public id?: number
  ){}
}

当我尝试更新数组 this.confirmPost = this.confirmPost.unshift(poste); 我收到一个错误:

ERROR in src/app/system/post/post.component.ts(39,51): error TS2345: Argument of type 'CreatePost[]' is not assignable to parameter of type 'CreatePost'.
  Type 'CreatePost[]' is missing the following properties from type 'CreatePost': data, category, user, title, and 4 more.
angularjs
  • 1 个回答
  • 10 Views
Martin Hope
Air
Asked: 2020-07-22 17:58:48 +0000 UTC

Angulsrjs 中的控制器和指令交互

  • 1

有一个指令

app.directive('rootElement', function () {
    return {
        restrict: "E",
        controller:'rootElementController',
        templateUrl: undefined,
        link: function (scope, element, attr, ctrl) { 
            console.log(scope.testScope.pages[5]) 
            this.templateUrl = scope.testScope.pages[5];
        }
    }
});

它与某个控制器交互并从后者接收一条线(路径是这样的frontend/pages/page-main/index.html),我需要它才能得到它。这是templateUrl.

我可以link更改函数中的值templateUrl吗?

我按照示例中给出的方法进行了尝试,但是唉,啊(没有用)...

这可以以某种方式实现吗?

提示后,像这样实现

app.directive('rootElement', function () {
    return {
        restrict: "E",
        controller:'rootElementController',
        templateUrl: '<ng-include src="{{testScope.pages[5]}}"></ng-include>',
        link: function (scope, element, attr, ctrl) { 

        }
    }
});

但我收到一个错误

在此处输入图像描述

angularjs
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5