角度选择对象数组

时间:2014-02-10 20:01:41

标签: javascript angularjs

我有一个名为presentations

的对象数组

我正在尝试在select元素中使用它们,但它不起作用。选择框只是空白,没有任何内容。

我认为这可能是因为它需要数组或对象,而不是对象数组。

我在做:

<select ng-model="secondPres" ng-options="p.name for p in presentations" ></select>

我可以在console.log中找到我的presentations数组并获取此信息:

0: Object
name: "club"
origName: "club"
slide: "1"
tenant: "grant"

1: Object
name: "2slide"
origName: "club"
slide: "1"
tenant: "grant"

2: Object
name: "CT_Stress_Test"
origName: "club"
slide: "1"
tenant: "grant"

那是怎么回事?

这是我的控制器代码。生成的console.log显示我上面的内容位于底部。在data-ng-init

上调用expand()
var socket = io.connect('https://xxxxxxx.xxxxxxxxxxxxx.net:3000');

$scope.versions =[];
$scope.presentations=[];
$scope.secondPres;
$scope.expand = function(){
    console.log("expand hit " + presentation1);
    var urlToSign = presentation1;
    var urlToSign = urlToSign.replace(/^(.*)\/Slide\d_v\d.PNG(.*)/,"$1"+"");
    console.log("url to sign = " + urlToSign);
    socket.emit('getSignedSlidesFromUrl',urlToSign);
    socket.on('signedUrls',function(data){
        console.log('got back signedUrls' + data);
        $scope.slides=data;
        $scope.$apply();

        var re = /^.*\/\/.*\/.*\/(.*)/;
        var fileName = re.exec(urlToSign)[1];

        for(i=0;i<presentationList.length;i++){
            var url = presentationList[i];
            console.log(url);
            var re = /^.*\/\/.*\/(.*)\/.*$/;
            var re2 = /^.*\/\/.*\/(.*)\/.*\//;
            var re3 =/^.*\/\/.*\/.*\/Slide(\d*)/;
            var fileName2 = re.exec(url)[1];
            var customerName = re2.exec(url)[1];
            var slideNum = re3.exec(url)[1];

            $scope.presentations.push({name:fileName2, origName:fileName, tenant:customerName, slide:slideNum});
        }
        console.log($scope.presentations);
    });
};

1 个答案:

答案 0 :(得分:1)

$scope.$apply()循环后,您刚刚错过了for,以使Angular看到您的新数组项。或者将事件处理程序的全部内容包装在传递给$scope.$apply()的函数中,类似于此处的snippit。

 socket.on('signedUrls',function(data){
    $scope.$apply(function() {
        console.log('got back signedUrls' + data);
        $scope.slides=data;

        var re = /^.*\/\/.*\/.*\/(.*)/;
        var fileName = re.exec(urlToSign)[1];

        for(i=0;i<presentationList.length;i++){
            var url = presentationList[i];
            console.log(url);
            var re = /^.*\/\/.*\/(.*)\/.*$/;
            var re2 = /^.*\/\/.*\/(.*)\/.*\//;
            var re3 =/^.*\/\/.*\/.*\/Slide(\d*)/;
            var fileName2 = re.exec(url)[1];
            var customerName = re2.exec(url)[1];
            var slideNum = re3.exec(url)[1];

            $scope.presentations.push({name:fileName2, origName:fileName, tenant:customerName, slide:slideNum});
        }
        console.log($scope.presentations);
    });
    });