为什么angularJs没有在页面加载时显示结果?

时间:2016-04-05 06:06:57

标签: php angularjs

我是AngularJs的新人。我试图从数据库中获取结果并使用angularjs在li中显示。我使用anguarjs filter并且工作正常。我的问题是脚本没有在页面加载时显示结果。

// Create connection
    $pdo=new PDO("mysql:dbname=dummy_db;host=localhost","root","");
    $statement=$pdo->prepare("SELECT * FROM mytable");
    $statement->execute();
    $results=$statement->fetchAll(PDO::FETCH_UNIQUE);
    $a = '';
    foreach($results as $__){
        $a .= json_encode($__).',';

    }

HTML和AngularJS

<body  ng-app="angapp"  ng-controller="angCtrl">
    <input type="text" ng-model="findname"  />
    <br />
    <ul>
        <li ng-repeat="stu in students | filter:findname">
            <img src="images/12.jpg" width="200" height="200"  />
            <span>Name :{{stu.u_name}}</span><br />
            <strong>Email :{{stu.u_mail}}</strong>
        </li>
    </ul>
<script>
    var testApp = angular.module('angapp',[]);
    testApp.controller('angCtrl',function($scope){
        $scope.students=[
            <?php echo $a; ?>
        ]

    });
</script>

Json Sample

$scope.students=[ {"u_name":"Adrian","0":"Adrian","u_mail":"Aenean.sed@ut.net"} ]

当我将文本放在input字段中时,上面的脚本工作正常,结果显示正确,但它没有在页面加载时显示结果。任何人都可以指导我,我可以解决这个问题。如果有人指导我,我很感激。谢谢。

1 个答案:

答案 0 :(得分:0)

你的代码工作得很好(angularjs side),只要确保你引用了angularjs。

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body  ng-app="angapp"  ng-controller="angCtrl">
    <input type="text" ng-model="findname"  />
    <br />
    <ul>
        <li ng-repeat="stu in students | filter:findname">
            <img src="images/12.jpg" width="200" height="200"  />
            <span>Name :{{stu.u_name}}</span><br />
            <strong>Email :{{stu.u_mail}}</strong>
        </li>
    </ul>
<script>
    var testApp = angular.module('angapp',[]);
    testApp.controller('angCtrl',function($scope){
       $scope.students=[ {"u_name":"Adrian","0":"Adrian","u_mail":"Aenean.sed@ut.net"} ];

    });
</script>
</body>
&#13;
&#13;
&#13;