【问题标题】:AngularJS ex.com/forum?id=1 to ex.com/#/forum?id=1AngularJS ex.com/forum?id=1 到 ex.com/#/forum?id=1
【发布时间】:2015-02-06 09:19:17
【问题描述】:

所以。我正在创建一个论坛,我想使用 AngularJS 来构建它。

首先。

现在,我已经建立了与我的数据库的连接:

<?php 
session_start();
$db = new mysqli("localhost","root","","uf") or die ("ERROR! With connection!");


function sql($query){
 return mysql_query($query);
}
$sql = "SELECT forum_id, forum_name, forum_desc, forum_created FROM forum_tbl";
if($query = $db->prepare($sql)){
	$query->bind_result($f_id, $f_name, $f_desc, $f_created);
	$query->execute();
	$query->store_result();
	
}else{
	echo $db->error;	
}
?>

分为2个文件,第一个是db_connect.php,第二个是core.php

我已经制作了上面的表格,它们有示例数据。

我现在如何将 f_id、f_name、.. 转换为 JSON 编码数据?并在我的 main.js 文件中使用该 JSON 来使用 data.forumid?

我已经使用 php 从数据库中读取数据:

		<?php if($query->num_rows !==0):
            while($row = $query->fetch()):
			?>
            <tr>
                <td><?= $f_id;?></td>
                <td><a href="#/HELPME"><?= $f_name;?></a></td>
                <td><?=  $f_desc;?></td>
                <td><?= $f_created;?></td>
            </tr>
            <?php endwhile; endif; ?>

但我不想这样读取数据。 我很想使用 ng-repeat。

【问题讨论】:

    标签: javascript php json angularjs forum


    【解决方案1】:

    您应该将行存储在一个数组中,然后使用 json_encode() 将其转换为 json:

    $rows = array();
    while($row = $query->fetch()) {
        $rows[] = $row;    
    }
    echo json_encode($rows);
    

    【讨论】:

    • 打印出 True:True。
    【解决方案2】:

    所以。

    我是这样做的......

    <a ng-click="setRoute('forum')" href="#/forum/id=<?php echo $f_id;?>" class="list-group-item">Link</a>
    

    在我的 html 中当然是 ng-app="urheilu"

    var app = angular.module('urheilu', ['ngRoute']);
    
    app.config(function($routeProvider) {
        $routeProvider
    .when('/forum/id=:id', {
    templateUrl: function(urlattr){
    return 'app/components/forums/threadlist.php?id=' + urlattr.id + '';
        } 
    })
    });
    

    这就是我进行动态链接的方式。

    完成。

    -凯文

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多