Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs未加载要选择的数据选项_Javascript_Angularjs_Json - Fatal编程技术网

Javascript Angularjs未加载要选择的数据选项

Javascript Angularjs未加载要选择的数据选项,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个select选项,需要从数据库(JSON)填充数据 但即使我有正确的json数据,它也无法填充 sample.html <div class="form-group form-group-sm"> <label class="sr-only" for="inputDoctype">Type</label> <select aria-describedby="basic-addon1" class="form-control" ng

我有一个select选项,需要从数据库(JSON)填充数据

但即使我有正确的json数据,它也无法填充

sample.html

<div class="form-group form-group-sm">
    <label class="sr-only" for="inputDoctype">Type</label>
    <select aria-describedby="basic-addon1" class="form-control" ng-model="matin.type" ng-options="x.type for x in data">
        <option value="{{x.type}}">{{x.type}}</option>
    </select>
</div>
列出doctype.php

<?php

require_once '/config/dbconfig.php';

$table = (isset($_GET['table'])) ? $_GET['table'] : NULL;

getById($table);

function getById() {
    $sql = "SELECT distinct type FROM min_in_head order by type asc";
    try {
        $db = getdb();
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $data = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($data);

    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}
?>

控制台结果

[{“type”:“Adj”},{“type”:“WC IN”}]


有人知道为什么吗?

首先,您的json似乎不符合逻辑,我也会更改它:

{"type":["Adj","CONS OUT"]}
但无论如何,您需要使用AngularHTTP服务来填充作用域!所以它是这样的:

$http.post('http://localhost/onseral/api/list-doctype.php').then(function(res) {
        $scope.matin= res.data;
    });
试试这个

<div class="form-group form-group-sm">
    <label class="sr-only" for="inputDoctype">Type</label>
    <select aria-describedby="basic-addon1" class="form-control" ng-model="matin.type" ng-options="x as x.type for x in data">
    </select>
</div>

类型
您可以使用它

<select class="form-control" ng-model="matin.type" ng-options="x.type for x in data">
     <option value ="">select one</option>
</select>

选择一个
没必要

<option value="{{x.type}}">{{x.type}}</option>
{{x.type}

uhh,当您使用
ng选项时,您不需要编写任何
,因为angular将生成它们,我用自己版本的json编写了它们!我只是想让您看看如何使用
http
服务。为此,您需要注入该服务并使用适当的json。如果你这样做,重写你的问题,我们可以帮助更多我只是不能得到你的json,我已经包括了php文件生成的json格式,生成的格式是不一样的你。json.encode有问题吗?使用
PDO::FETCH_ASSOC
而不是
PDO::FETCH_OBJ
即使使用PDO::FETCH_ASSOC也能获得相同的json结果。您能创建plunker演示吗?
<option value="{{x.type}}">{{x.type}}</option>