我试图在与我的服务文件相同的目录中加载本地json文件.
没有JS错误,在Net选项卡下,我可以看到json文件已加载.
然而,将响应txt设置为var"static_obj"不会加载JSON数据.当我有JSON数据硬编码var WEBTEST = {}时,它加载得很好.为什么?
'use strict';
webTestApp.factory('webtest', function ($q, $timeout) {
var Webtest = {
//Methods exposed for the class
fetch: function(callback){
//fetch the data from below hard coded
var deferred = $q.defer();
var static_obj = [];
var promised = deferred.promise;
var xobj = new XMLHttpRequest();
//giving an artificial delay of .03 seconds..
//controller will render the page & will show the service data after .03 seconds
$timeout(function(){
xobj.overrideMimeType("application/json");
xobj.open('GET', 'angular/scripts/services/webtest.json', true);
xobj.onreadystatechange = function () {
if …Run Code Online (Sandbox Code Playgroud) 感谢@rijin 重写代码:reducers/index2.ts:
import { ActionReducerMap } from "@ngrx/store";
import { userReducer, UserState } from "./user2.reducer";
import { cartReducer, CartState } from "./cart2.reducer";
interface AppState {
user: UserState;
cart: CartState;
}
export const reducers2: ActionReducerMap<AppState> = {
user: userReducer,
cart: cartReducer
};
Run Code Online (Sandbox Code Playgroud)
用户处的编译错误:userReducer:
Type '(appUserState: UserState, action: any) => User' is not assignable to type 'ActionReducer<UserState, Action>'.
Property 'user' is missing in type 'User' but required in type 'UserState'.
Run Code Online (Sandbox Code Playgroud)
/reducers/user.ts:
export class User {
uName: string;
isAdmin: boolean;
ts: string; …Run Code Online (Sandbox Code Playgroud) 我不知道为什么这不起作用.计数返回时阵列的大小正确.
这是mWeb.json文件:
[{"Title": "Some-Name","h1": "Some-h1-words", "Comments": "Some Comment", "Comments2": "Some Comment2"},
{"Title": "Some-Name b","h1": "Some-h1-words b", "Comments": "Some Comment b", "Comments2": "Some Comment2 b"},
{"Title": "Some-Name c","h1": "Some-h1-words c", "Comments": "Some Comment c", "Comments2": "Some Comment2 c"}]
Run Code Online (Sandbox Code Playgroud)
这是代码:
<?
$url = 'mWeb.json';
$JSON = file_get_contents($url);
$someName = "Some-Name";
$data = json_decode($JSON);
echo count($data);
for($x=0; $x<count($data); $x++){
if($data[$x]['Title']==$someName){
echo '[{"Title":"'.$data[$x]["Title"].'","h1":"'.$data[$x]["h1"].'","Comments":"'.$data[$x]["Comments"].'","Comments2":"'.$data[$x]['Comments2'].'"}]';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
所有的回声都是数组的长度 - 回声计数($ data);
正如您在主题中看到的那样,我收到此错误是因为数据库中的一列是一串 JSON,我想我可以通过在 ngFor 循环中使用 ngFor 循环来解决这个问题。
以下是html:
<tbody>
<tr *ngFor="let item of mf.data">
<td>{{ item.id }}</td>
<td>{{ item.user_id }}</td>
<td class="text-right">{{ item.orders_id }}</td>
<td>
<ul *ngFor="let x of item.product">
<li>{{ x.name }}</li>
<li>{{ x.price }}</li>
<li>{{ x.category }}</li>
<li>{{ x.ts }}</li>
<li>{{ x.enabled }}</li>
<li>{{ x.counter }}</li>
</ul>
</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
以下是“产品”列的一行的样子:
[
{
"id": "13",
"name": "test 5",
"price": "3.42",
"category": "chocolates",
"ts": "2019-01-08 10:41:15",
"product_image_id": "50",
"enabled": "1",
"product_image": "40-64-grand-canyon.png",
"counter": "2"
},
{
"id": "18",
"name": "test …Run Code Online (Sandbox Code Playgroud)