用angular調用PHP中的數據,點擊按鈕不能執行js函數,原生JS也試過,失敗。

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

</head>
<style>

*{margin: 0;padding:0;}
table{width:100%;height:auto;text-align: left;}
tr{border:1px solid #eee;}
table,tr,td{border-collapse: collapse;}
tr>th{width:25%;height:auto;}
.app_save_box{width:50px;height:30px;background:#00B83F;color:#fff;text-align: center;line-height: 30px;margin: 20px auto;}

</style>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">

<form action="">
    <table>
        <tr>
            <th>序号</th>
            <th>应用名</th>
            <th>网址</th>
        </tr>
        <tr ng-repeat="x in names">
            <td>{{ x.id }}</td>

            <td>
                <input type="text" value="{{ x.name }}" disabled="disabled"/>
                <button class="app_name_change">修改</button>
            </td>
            <td>
                <input type="text" value="{{ x.url }}" disabled="disabled"/>
                <button class="app_url_change">修改</button>
            </td>
            <td>
                <button class="app-delete">删除该项</button>
            </td>
        </tr>
    </table>
</form>
<div class="app_save_box">保存</div>

</div>

<script>

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("data.php")
            .then(function (result) {
                $scope.names = result.data.records;
            });
});

$(".app_name_change").on("click",function(){
    $(this).prev().removeAttr("disabled")
})

</script>
</body>
</html>


php代碼

<?php
echo <<<EOT
{
"records":[
{"id":"1","name":"新浪","url":"http://www.sina.com"},
{"id":"2","name":"百度","url":"http://www.baidu.com"}
]
}
EOT;
?>

阅读 2.3k
3 个回答

原因是angular调用PHP上的数据时,JS先执行,所以无法绑定相应的标签,无法执行点击事件。

添加header('Content-Type: application/json');让输出的内容是json

<?php
header('Content-Type: application/json');

echo <<<EOT
{
"records":[
{"id":"1","name":"新浪","url":"http://www.sina.com"},
{"id":"2","name":"百度","url":"http://www.baidu.com"}
]
}
EOT;

动态生成的dom是不能这样绑定事件的,你可以使用事件委托,或者直接绑定ng-click

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题