在Angular中:我无法从$ scope的属性中获取值

时间:2015-04-21 15:43:27

标签: javascript angularjs

页面上有一个按钮,我希望通过不同的属性值来控制它显示或隐藏:

<a href="" role="button" class="btn-unworking" ng-show="d.status === '0'" ng-click="cancel(d.from)">cancel</a>

<a href="" role="button" class="btn-working" ng-show="d.status === '1'"></a>

<a href="" role="button" class="btn-done" ng-show="d.status === '-1'"></a>

这是我的数据对象:

$scope.designedTimeList.push({
            from: $scope.fromTime,
            duration: $scope.durationTime,
            theme: $scope.theme,
            plan: $scope.plan,
            status: '0'
        });

现在,我希望匹配fromTime和实时之间的时间:

var date = new Date();

        var hour = date.getHours();
        var minute = date.getMinutes();

        var existHour = +($scope.designedTimeList[0].from.subString(0, 2));
        var existMinute = $scope.designedTimeList[0].duration;
        // i just wanna get the first element in the `designedTimeList`

        if (existHour == hour && existMinute >= minute) {
            $scope.designedTimeList[0].status = '1';
        } else if (existHour < hour || (existHour == hour && existMinute < minute)) {
            $scope.designedTimeList[0].status = '-1';

        }

但是当我使用它时:

$scope.designedTimeList[0].from.subString(0, 2);
$scope.designedTimeList[0].duration;

记录TypeError: Cannot read property 'from' of undefinedTypeError: Cannot read property 'duration' of undefined

$ scope.designedTimeList中的数据是这样的:

[{duration: "15 min",from: "2 am",plan: "test",status: "0",theme: "test"},{},{}...]

请告诉我问题是什么以及如何弄清楚。

取消代码:

    var checkTime = function() {

        var date = new Date();

        var hour = date.getHours();
        var minute = date.getMinutes();

        // var temp = $scope.



            var existHour = +($scope.designedTimeList[0].from.subString(0, 2));
            var existMinute = $scope.designedTimeList[0].duration;

            if (existHour == hour && existMinute >= minute) {
                $scope.designedTimeList[0].status = '1';
            } else if (existHour < hour || (existHour == hour && existMinute < minute)) {
                $scope.designedTimeList[0].status = '-1';

            }

            console.log($scope.designedTimeList[0]);

            $timeout(checkTime, 1000 * 60 * 1);


    }

            $timeout(checkTime,0);

1 个答案:

答案 0 :(得分:0)

在推入数组之前,你必须告诉$ scope.designedTimeList是一个数组。 所以你必须写

$scope.designedTimeList = [];

之后

$scope.designedTimeList.push({
        from: $scope.fromTime,
        duration: $scope.durationTime,
        theme: $scope.theme,
        plan: $scope.plan,
        status: '0'
    });

另一件事,你必须编写susbstring而不是&#34; subString&#34; 即

$scope.designedTimeList[0].from.substring(0, 2);