简体   繁体   English

Angular JS ng-repeat无法正常工作

[英]Angular JS ng-repeat not working as expected

I have a controller like this: 我有一个像这样的控制器:

controller('BreadCrumbs', ['$scope','crumble','$rootScope', function ($scope,crumble,$rootScope) {    
    function init (){
        $scope.ui={};
        $scope.ui.mdBreadCrumbs=[{"path":"path1","label":"label1"}];     
        $rootScope.oldScope=$scope;
    }
    $scope.setBreadCrumbs=function() {        
        $scope.ui.mdBreadCrumbs=crumble.trail;    
    } 
    init();
}]);

and in HTML, 在HTML中,

<ol id="breadCrumbList"  ng-controller="BreadCrumbs as bcrmbs">
 {{ui.mdBreadCrumbs}}
  <li ng-repeat="bc in ui.mdBreadCrumbs">
    <a ng-href="{{bc.path}}">{{bc.label}}</a>
  </li>
</ol>

{{ui.mdBreadCrumbs}} is showing some like [{"path":"path1","label":"label1"}] . {{ui.mdBreadCrumbs}}显示了类似[{"path":"path1","label":"label1"}] But in ng-repeat, it is not iterating. 但是在ng-repeat中,它不是迭代的。 Using $scope.setBreadCrumbs I put some more values, but still ngRepeat not working. 使用$scope.setBreadCrumbs我输入了更多值,但是ngRepeat仍然无法正常工作。

Anyone have any idea why it is not working? 有人知道为什么它不起作用吗?

Change 更改

<ol id="breadCrumbList"  ng-controller="BreadCrumbs as bcrmbs">

to

<ol id="breadCrumbList"  ng-controller="BreadCrumbs">

DEMO DEMO

 var app = angular.module('app', []); app.controller('BreadCrumbs', ['$scope', function($scope) { $scope.ui = {}; $scope.ui.mdBreadCrumbs = [{ "path": "path1", "label": "label1" }]; }]); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body ng-app="app" ng-controller="BreadCrumbs"> <ol id="breadCrumbList"> {{ui.mdBreadCrumbs}} <li ng-repeat="bc in ui.mdBreadCrumbs"> <a ng-href="{{bc.path}}">{{bc.label}}</a> </li> </ol> <script type=" text/javascript " src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js "></script> <script type="text/javascript " src="MainViewController.js "></script> </body> </html> 

You have to change something in your HTML and your HTML should be like this: 您必须更改HTML中的某些内容,并且HTML应该如下所示:

<div ng-app>
  <div ng-controller="BreadCrumbs">
      <div ng-repeat="bc in ui.mdBreadCrumbs">
          <a ng-href="{{bc.path}}">{{bc.label}}</a>
      </div>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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