简体   繁体   English

数组的AngularJS ng-repeat不起作用

[英]Angularjs ng-repeat for array not working

I have array that creates via this script: 我有通过此脚本创建的数组:

    $scope.bindModel = function(data) {
        $scope.model = data;
        $scope.projects = projectNames();
        $scope.$apply($timeout($scope.bindModel(data)));
    };

    /* Function that enumerates the names of the project in the list of model's object*/
    var projectNames = function () {
        var array = [];
        // SOME CODE . . .
        return array;
    };

And it's creates array like this: 它创建像这样的数组:

$scope.projects = ["first project", "second project", "third project"];

And when I try to show it on page via ng-repeat="project in projects", nothing not shows, but in debug console massive have values. 当我尝试通过ng-repeat =“ project in projects”在页面上显示它时,什么都没有显示,但是在调试控制台中,大量的都有值。 Where did I go wrong? 我哪里做错了? I use this in select: 我在选择中使用此:

<select class="chzn-border" style="width:98%">
     <option disabled selected style='display:none;'>Choose a project...</option>
     <option ng-repeat="project in projects" value="{{project}}">{{project}}</option>
</select>

JSFiddle 的jsfiddle

According to me angular is unaware of the changes made to $scope.Projects. 据我介绍,angular并没有意识到对$ scope.Projects所做的更改。 So you should call $scope.bindModel in apply. 因此,您应该在apply中调用$scope.bindModel Try using timeout which will wrap your code in $scope.$apply. 尝试使用超时,该超时会将您的代码包装在$ scope。$ apply中。

$timeout($scope.bindModel(data));

Hopefully this will help. 希望这会有所帮助。

I can give you a hint from where you can get idea to solve your ng-repeat problem: 我可以给您一个提示,从那里您可以找到解决ng-repeat问题的想法:

HTML: HTML:

<div ng-app ng-controller="MyController">
    <input type="submit" ng-click="projectNames()" value="submit"></input>
    <select class="chzn-border" style="width:98%">
     <option disabled selected style='display:none;'>Choose a project...</option>
     <option ng-repeat="project in projects" value="{{project}}">{{project}}</option>
</select>

Angular JS: Angular JS:

function MyController($scope) {
  // $scope.bindModel = function(data) {
        //$scope.model = data;
        //$scope.projects = projectNames();
    //alert($scope.projects);
    //};

    $scope.projects = [];
    $scope.projectNames = function () {
        $scope.projects.push(" a is a project.");
        $scope.projects.push(" b is a project.");
        $scope.projects.push(" c is a project.");
    };

}

Here i have used a button that is helping to load the array and then in html ng-repeat is applied on array to show the data. 在这里,我使用了一个有助于加载数组的按钮,然后在html中将ng-repeat应用于数组以显示数据。 You can load array on load like you want. 您可以根据需要在加载时加载数组。

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

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