初始化数组javascript,角度控制器

时间:2014-07-03 16:00:06

标签: javascript angularjs

myModule.controller('MetadataController', function () {
        this.allOrganisations = [{name:"test"}, {name:"alpha"}, {name:"beta"}];
        var arrayLength = this.allOrganisations.length;
        for (var i = 0; i < arrayLength; i++) {
            alert(this.allOrganisations[i].name);
        }
});

正在运作,但在视图中什么都没有出来?

<form name="form" novalidate ng-controller="MetadataController"  class="form-inline">
    <select ng-model="form.metadata.title.organisation" ng-options="organisation.name for organisation in form.AllOrganisations"></select><br>
    <ul >
      <li ng-repeat="o in form.allOrganisations">{{ o.name }}</li>
    </ul>

1 个答案:

答案 0 :(得分:0)

请参阅此处http://jsbin.com/wewiv/1/

  myModule.controller('MetadataController', function ($scope) {
            $scope.allOrganisations = [{name:"test"}, {name:"alpha"}, {name:"beta"}];
            var arrayLength = $scope.allOrganisations.length;
            for (var i = 0; i < arrayLength; i++) {
                alert($scope.allOrganisations[i].name);
            }
    });

HTML:

 <select ng-model="form.metadata.title.organisation" ng-options="organisation.name for organisation in allOrganisations"></select><br>
    <ul >
      <li ng-repeat="o in allOrganisations">{{ o.name }}</li>
    </ul>
      </div>