简体   繁体   中英

Angular is not displaying data in table row. ng-repeat is not working

I'm trying to use Angularjs. The problem is, ng-repeat not working when it is in the route. It works when it is not in route.

Here's what I have so far: See the links..

this works well... http://plnkr.co/edit/1syQFJdMyRUYncBrREue?p=preview

but in this, it didn't work now..(navigate in persons) http://plnkr.co/edit/gsi2mZpnU0YJUUOa20DO?p=preview

html

<html ng-app="myApp">
<head>
    <title>Confirm Dialog Box</title>
 <!-- 
    in the link above i create a custom dialog box
 -->
</head>
<body>

<div ng-controller="loglistController">
<table>
  <tr ng-repeat="x in names">
    <td onclick="showDialog()" ng-click="showInEdit(x)">{{ x.Name }}</td>
    <td onclick="showDialog()" ng-click="showInEdit(x)">{{ x.Country }}</td>
  </tr>
</table>


             <div id="white-background">
</div>
<div id="dlgbox">
    <div id="dlg-header"><h3>Information</h3></div>
    <div id="dlg-body">
      Name <input type="text" ng-model="selectedPerson.Name" /><br/>
      Country <input type="text" ng-model="selectedPerson.Country" /><br/>


    </div>
    <div id="dlg-footer">
        <button onclick="dlgOK()">Update</button>
        <button onclick="dlgCancel()">Exit</button>
    </div>
</div>

angular

var myApp = angular.module('myApp', ['ngRoute']);

myApp.controller('loglistController', ['$scope', '$http', function ($scope, $http) {

    $scope.data=[];
    $scope.selectedPerson={ Name:"",Country:""}; 

            $http.get("loglistajax.php")
                .then(function (response) {$scope.names = response.data.records;});

    $scope.showInEdit=function(person){
    $scope.selectedPerson = person;
    };



 }]);

Here is my version

I agree with most points that imbalind mentioned.

Just would like to add few more:

  • we should not instantiate same app multiple times (in original Plunker we had it in both index.html and list.html). Here are two cases:

     var myApp = angular.module('myApp', ['ngRoute']); //new instance of 'myApp' var myApp = angular.module('myApp'); //simply handle to 'myApp' 
  • we had two 'loglistController' that belong to same angular.module , which not going to work

  • angular itself was loaded in both html files, not good.

  • for that particular Plunker, fixed route in app.js:

     .when('/', { templateUrl: '/index.html', controller: 'mainController' }) 
  • and please don't mixup jQuery script, it will create a lot of confusion later...

Here is a (somewhat) fixed plunker , however I found the following issues in your code:

  • You have double controllers: loglistController is defined twice, once inside app.js and then again inside list.html .

    You wrote the following line

    <tr ng-repeat="x in data">

    Thinking it would read from loglistController 's $scope.data but your app seems to be considering the first controller, so your controller scope has no data properties. I solved replacing the controller in app.js with the one inside list.html .

    Thinking about it, I guess you are not allowed to put a route controller's code inside it's template, so you'd better avoid doing that in the future!

  • Your app.js ' templateUrl does not exists when url is '/' and this throws an error. I solved by replacing it with 'list.html' . I'm quite sure it's not what you will want, but it was needed to make it work.

are you sure your $http request is resolving?

 $http.get("loglistajax.php").then(function (response) {
     $scope.names = response.data.records;
 }).catch(function(response) {
     console.log("I am failing to resolve a non-2xx response");
 });

initially try without the $http request using dummy data n build up slowly!

FYI ...

you can do ...

$scope.names = $http.get("loglistajax.php");

Which only works if the HTTP request resolve!

PS neither of your plunkr's show anything!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM