Angular不在表行中显示数据。 ng-repeat不起作用

时间:2015-12-18 17:04:45

标签: javascript php angularjs

我试图使用Angularjs。问题是,ng-repeat在路线中不起作用。它在不在路线时有效。

这是我到目前为止所拥有的: 请参阅链接..

这很有效...... http://plnkr.co/edit/1syQFJdMyRUYncBrREue?p=preview

但在此,它现在没有工作..(导航人) http://plnkr.co/edit/gsi2mZpnU0YJUUOa20DO?p=preview

HTML

{{1}}

{{1}}

3 个答案:

答案 0 :(得分:2)

这是我的version

我同意大多数points提到的 imbalind

只想补充一点:

  • 我们不应该多次实例化同一个应用程序(在原始的Plunker中,我们在index.html和list.html中都有它)。以下是两种情况:

    var myApp = angular.module('myApp', ['ngRoute']); //new instance of 'myApp' 
    
    var myApp = angular.module('myApp'); //simply handle to 'myApp'
    
  • 我们有两个'loglistController'属于同一个angular.module,但无法正常工作

  • angular本身已加载到两个html文件中,不太好。

  • 针对特定的Plunker,app.js中的固定路线:

    .when('/', {
        templateUrl: '/index.html',
        controller: 'mainController'
    })
    
  • 请不要混淆jQuery脚本,以后会造成很多混乱......

答案 1 :(得分:1)

以下是(有些)fixed plunker,但我在您的代码中发现了以下问题:

  • 您有双控制器:loglistController定义两次,一次在app.js内,然后再在list.html内。

    你写了以下一行

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

    认为它会从loglistController的{​​{1}}读取,但您的应用似乎正在考虑第一个控制器,因此您的控制器范围没有数据属性。我解决了将$scope.data中的控制器替换为app.js内的控制器。

    考虑到这一点,我猜你不允许将路由控制器的代码放在它的模板中,所以你最好避免在将来这样做!

  • 当网址为list.html时,您的app.js'templateUrl不存在,并且会引发错误。我用'/'替换它解决了。我很确定这不是你想要的,但需要让它发挥作用。

答案 2 :(得分:0)

你确定你的$ http请求正在解决吗?

 $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");
 });

最初在没有$ http请求的情况下尝试使用虚拟数据n慢慢建立!

FYI ......

你可以做......

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

仅在HTTP请求解决时才有效!

PS你的两个傻瓜都没有显示任何东西!