嵌套的函数不适用于角度js

时间:2014-07-26 18:04:49

标签: angularjs

当我使用以下内容时:

$scope.test = function () {
        this.batchId = false; 
        this.batchToggle = true; 
    }   

批处理ID工作,ng-show和mouseover事件发生并起作用。 $ scope.test函数在每个批次id上显示upload.view。

但是当我使用它时:

$scope.searchFeature = {
    showSearch: false, 
            addBatchButton: true, 
            test:function() {
                // alert(this);     
                this.batchId = false;
                this.batchToggle = true; 
            }

showSearch.test()不起作用。并且没有显示批量ID上传/查看悬停为什么?

See plunker code.

2 个答案:

答案 0 :(得分:0)

正在运作..检查this plunker

您需要更改通话ng-mouseover="searchFeature.test()"

由于函数test()是在searchFeature对象中定义的,您需要使用searchFeature访问它。

它的javascript工作方式与角度无关......

修改

签出updated plunker,它正在工作..我不得不对此进行一些更改...让我知道它是否按预期工作

答案 1 :(得分:0)

试一试。希望这是你所追求的功能:

  <td data-title="'ID'" ng-mouseover="test(batch)" ng-mouseleave="testLeave(batch)">
          <div ng-show="batch.showId">{{batch.id}}</div>
          <div ng-show="batch.toggle">
            <a>Upload</a>  <a ng-href="/batches/{{batch.id}}">View</a> 
          </div>
        </td>

    $scope.test = function (batch) {
        batch.showId = false; 
        batch.toggle = true; 
    }   

    $scope.testLeave = function (batch) { 
        batch.showId = true; 
        batch.toggle = false; 
    }   


$scope.batches = [
        {id: "2014BATCH50", status: "QC in Progress. Illumina: 24/50",  date: "10/24/14", qlty: "check", illumina: "none, raw, modified, modified", FX: "modified", showId: true, toggle: false},
        {id: "2014BATCH49", status: "Pre-Extraction",  date: "10/24/14", qlty: "check", illumina: "none, raw, modified, modified", FX: "modified", showId: true, toggle: false}

    ];

为了速度,我减少了批次数。为每个批次分配 showId 切换属性,并直接处理它们。如果您愿意,也可以将其折叠到 $ scope.searchFeature 结构中:

$scope.searchFeature = { 
        showSearch: false, 
        addBatchButton: true, 
        test:function(batch) {      
            batch.showId = false; 
            batch.toggle = true; 
        }
    }