为什么jsFiddle上的这个angularjs不起作用

时间:2012-12-03 21:12:29

标签: angularjs

http://jsfiddle.net/maxl/tNZAm/102/

我希望转发器列出这个数组:

  

function MyControl(){       var self = this;

self.values = ["a","b","c","d","e","f"];
self.selectedIndex = -1;

self.toggleSelect = function(ind){
    if( ind === self.selectedIndex ){
        self.selectedIndex = -1;
    } else{
        self.selectedIndex = ind;
    }
}

self.getClass = function(ind){
    if( ind === self.selectedIndex ){
        return "selected";
    } else{
        return "";
    }
}

self.getButtonLabel = function(ind){
    if( ind === self.selectedIndex ){
        return "Deselect";
    } else{
        return "Select";
    }
} }​

1 个答案:

答案 0 :(得分:1)

您应该使用$scope注入来将数据附加到DOM。此外,您正在加载Angular.js两次(一次通过“Choose Framework”下拉列表,一次通过“Add Resources”面板),导致一个非常非常奇怪的错误。

这是一个有效的jsFiddle:http://jsfiddle.net/BinaryMuse/tNZAm/103/

function MyControl($scope){
    $scope.values = ["a","b","c","d","e","f"];
    $scope.selectedIndex = -1;

    $scope.toggleSelect = function(ind){
        if( ind === $scope.selectedIndex ){
            $scope.selectedIndex = -1;
        } else{
            $scope.selectedIndex = ind;
        }
    }

    $scope.getClass = function(ind){
        if( ind === $scope.selectedIndex ){
            return "selected";
        } else{
            return "";
        }
    }

    $scope.getButtonLabel = function(ind){
        if( ind === $scope.selectedIndex ){
            return "Deselect";
        } else{
            return "Select";
        }
    }
}​