角度构建一个带有for循环的数组

时间:2015-06-12 23:15:27

标签: javascript arrays angularjs

我正在测试角应用程序中动态构建数组。基本上我有一个范围函数,它生成一组人并返回数组,我在数组上有一个ng-repeat。数组没有显示,但我也没有在控制台中出现任何错误,所以idk是什么了:

我是否正确调用getPerson函数?如果有更好的方法,请告诉我。

heres the fiddle as well

$scope.person = {
        firstname: "",
        lastname: "",
        isActive: true,
        fullname: function() {
            var personobject;
            personobject = $scope.person;
            return personobject.firstname 
                + " " 
                + personobject.lastname;
        }
    };

$scope.people = function() {
        var pplArray = [];
        var firstnames = ['abdul','mahmud','gasser','ibtihaj','abudi'];
        var lastnames = ['ahmad','samrai','badawi','jasim','ahmad'];
        var actives = [true,true,false,true,false];
        for (var i = 0; i < firstnames.length; i++) {
            pplArray[i] = getPerson(firstnames[i], lastnames[i], actives[i]);
        }
        return pplArray;
    };
$scope.getPerson = function(first, last, active) {
        var newPerson = $scope.person;
        newPerson.firstname = first;
        newPerson.lastname = last;
        newPerson.isActive = active;
        return newPerson;
    };

1 个答案:

答案 0 :(得分:2)

我在这里更新了你的小提琴:https://jsfiddle.net/7j2khgbj/2/

var myapp = angular.module("myapp", []);
myapp.controller('appCont', function($scope) {

    var Person = function(){
        this.firstname = "";
        this.lastname = "";
        this.isActive = true;

    };

    Person.prototype.fullname = function() {
            return this.firstname 
                + " " 
                + this.lastname;
    };

    var getPerson = function(first, last, active) {
        var newPerson = new Person();
        newPerson.firstname = first;
        newPerson.lastname = last;
        newPerson.isActive = active;
        return newPerson;
    };    

    $scope.addPerson = function() {
        $scope.people.push({
            firstname: $scope.person.firstname,
            lastname: $scope.person.lastname
        });
        $scope.person.firstname = '';
        $scope.person.lastname = '';

    };

    $scope.people = (function() {
        var pplArray = [];
        var firstnames = ['abdul','mahmud','gasser','ibtihaj','abudi'];
        var lastnames = ['ahmad','samrai','badawi','jasim','ahmad'];
        var actives = [true,true,false,true,false];
        for (var i = 0; i < firstnames.length; i++) {
            pplArray[i] = getPerson(firstnames[i], lastnames[i], actives[i]);
        }
        return pplArray;
    })();


    /*$scope.people = [
        {firstname: 'abdul', lastname: 'ahmad'},
        {firstname: 'mahmud', lastname: 'samrai'},
        {firstname: 'gasser', lastname: 'badawi'},
        {firstname: 'ibtihaj', lastname: 'jasim'},
        {firstname: 'abudi', lastname: 'ahmad'},
        {firstname: 'ahmad', lastname: 'jasim'},
        {firstname: 'abdul', lastname: 'samrai'}
    ];*/
});

我看到的一些问题:

1)$ scope.people是一个函数,而不是一个数组(所以我只是执行它并保存结果)

2)你总是覆盖这个人(你需要一个为数组元素创建新实例的Person类,而不是用新数据覆盖同一个实例 - 这样你就可以在所有数组元素中得到相同的东西)

3)在$ scope上你应该放置需要从视图中访问的东西。辅助函数可以只是控制器中的本地函数(如果您不希望它们作为服务,尽管它们是可重用的服务)

4)通过ng-repeat跟踪(如果是重复键)