2016-09-21 32 views
0

大街創建了一個應用程序在我的MySQL數據庫,以顯示所有用戶更新,一切工作正常,但是當我的PHP代碼返回包含數據的JSON範圍更新,但我沒有得到任何展示我AngularJS範圍並不在模型

<!Doctype html> 
 
<html> 
 
    <head> 
 
\t  <title>Simple Dynamic Score Board | By Kascique Lowmans</title> 
 
\t \t <meta charset="utf-8"/> 
 
       <link rel="stylesheet" href="style.css"/> 
 
\t  <script src="http://code.angularjs.org/1.2.13/angular.js"></script> 
 
\t \t <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
\t </head> 
 
\t <script> 
 

 
\t   var webapp = angular.module('myapp', []); 
 
\t \t \t webapp.controller('myusers', function($scope, $http){ 
 
    \t \t \t  $scope.users = []; 
 
\t \t \t \t $http.get("selectdata.php").then(function(response){ 
 
\t \t \t \t  $scope.users = response.data; 
 
            $scope.$apply(); 
 
            alert($scope.users); 
 
             
 
\t \t \t \t }); 
 
           // $scope.$watch("users", function(){ alert('users scope changed');}); 
 
\t \t \t \t 
 
\t \t \t }); 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t  </script> 
 
\t <body data-ng-app="myapp" > 
 
\t <div class="control-group"> 
 
\t \t \t <h1>Select a user</h1> 
 
\t \t \t <div data-ng-controller="myusers" > 
 
\t \t \t \t <label class="control control--radio" data-ng-repeat="x in users"><span>{{$index + 1}}</span> {{ x.firstname +' '+x.lastname }} <b>{{ x.score}}</b> 
 
\t \t \t \t \t <input type="radio" name="radio"/> 
 
\t \t \t \t \t <div class="control__indicator"></div> 
 
\t \t \t \t </label><br/> 
 
           <h1 id="trail"></h1> 
 
\t \t \t \t <button ng-click="click()">+ Add score</button> 
 
\t \t \t </div> 
 
\t \t \t <footer> 
 
       &copy; copyright <a href="http://www.kasciquelowmans.ml/">Kascique Lowmans</a> 
 
      </footer> 
 
\t </div> 
 
\t </body> 
 
</html>

+0

你有一個理由使用的角度非常非常過時的版本? –

+0

不,我只是試過應該仍然可以工作 –

回答

1

你還沒有指定什麼NG-模型。糾正我,如果我錯了,但我沒有看到你的整個代碼中的任何ng模型。

+0

爲什麼他需要ng-model來顯示用戶數據? –

+0

這個問題並不清楚,但我相信他正嘗試將用戶添加到某個數組中,因爲有按鈕分配的ng鍵功能。 – MSH

+0

對不起我的錯誤我的意思是在我的表達 –

0

你如何改變你的範圍?通過click()函數?你可以發佈嗎?
順便說一句,你不需要$scope.$apply()內部的角度操作(在這種情況下$ http.get),因爲它會自動執行此操作。

1

你在你的代碼

1)不正確的HTML 2次失誤。頭標籤包括身體標籤。 2)你不應該從$ http promise回調中調用$ scope.apply,因爲它會被Angular調用。

下面是一個略有變化的剪裁,它的工作原理。

var webapp = angular.module('myapp', []); 
 
\t \t \t webapp.controller('myusers', function($scope, $http){ 
 
    \t \t \t $scope.users = []; 
 
\t \t \t \t $http.get("https://randomuser.me/api/?results=10").then(function(response){ 
 
        $scope.users = response.data.results; 
 
             
 
\t \t \t \t }); 
 
\t \t \t \t $scope.regUser = function(index){ 
 
\t \t \t \t \t  $scope.thereguser = index; 
 
\t \t \t \t } 
 

 
\t \t \t \t 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="control-group" data-ng-app="myapp"> 
 
\t \t \t <h1>Select a user</h1> 
 
\t \t \t <div data-ng-controller="myusers" > 
 
\t \t \t \t <label class="control control--radio" data-ng-repeat="x in users"><span>{{$index + 1}}</span> {{ x.name.first }} <b>{{ x.score}}</b> 
 
\t \t \t \t \t <input type="radio" name="radio"/> 
 
\t \t \t \t \t <div class="control__indicator"></div> 
 
\t \t \t \t </label><br/> 
 
           <h1 id="trail"></h1> 
 
\t \t \t \t <button ng-click="click()">+ Add score</button> 
 
\t \t \t </div> 
 
\t \t \t <footer> 
 
       &copy; copyright <a href="http://www.kasciquelowmans.ml/">Kascique Lowmans</a> 
 
      </footer> 
 
\t </div>

+0

對不起,我注意到我已經犯了這個HTML錯誤,但讓我們忽略它。你看到那裏的ng-click是另一個功能工作正常,所以只是忽略它 –

+0

也許是它的我的php代碼,但我沒有看到它的任何錯誤 –

+0

好吧,試着改變你的代碼來做和我的例子一樣。它應該適合你。比逐步更換網址和綁定找到錯誤的地方。 –