我是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($__).',';
}
<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>
$scope.students=[ {"u_name":"Adrian","0":"Adrian","u_mail":"Aenean.sed@ut.net"} ]
当我将文本放在input
字段中时,上面的脚本工作正常,结果显示正确,但它没有在页面加载时显示结果。任何人都可以指导我,我可以解决这个问题。如果有人指导我,我很感激。谢谢。
答案 0 :(得分:0)
你的代码工作得很好(angularjs side),只要确保你引用了angularjs。
<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;