AngularJs - 如何在promise的控制器外使用函数变量

时间:2016-10-12 08:32:01

标签: javascript angularjs angular-ui-bootstrap

service.js

.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {

    var promise = tableService.getTable();

    promise.then(
         function(payload) {                    
            data = JSON.stringify(payload.data);
            alert(data);
        },
        function(errorPayload) {
            $log.error('failure loading movie', errorPayload);
        });

            //Editable
    this.tableEdit = new ngTableParams({
            page: 1,            // show first page
            count: 10           // count per page
        }, {
            total:data.length, // length of data
            getData: function($defer, params) {
                $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });
})

controller.js

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections{
    if(tableView == table)
    {
        if(isFilter)
        {
            return [searchArray count];
        }
        else
        return [idarray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == table)
    {
        if(isFilter)
        {
            cell.textLabel.text=[NSString stringWithFormat:@"%@",[searchArray objectAtIndex:indexPath.row]];
        }
        else
        {
            cell.textLabel.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(tableView == table)
        {
            if(isFilter)
            {
                txt.text=[searchArray objectAtIndex:indexPath.row];
                table.hidden=YES;
                txt.enabled=NO;
                txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
                idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
                EmployeeID=idlbl.text;
                EmployeeName=txt.text;

            }
            else
            {
                txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
                idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
                table.hidden=YES;
                EmployeeID=idlbl.text;
                EmployeeName=txt.text;
                txt.enabled=NO;
            }

        }
}

-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;
    [self updateSearchArray:searchTextString];
}
-(void)updateSearchArray:(NSString *)searchText
{
    if(searchText.length==0)
    {
        isFilter=NO;
    }
    else{

        isFilter=YES;
        searchArray=[[NSMutableArray alloc]init];
        for(NSString *string in namearray){

            NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(stringRange.location !=NSNotFound){

                [searchArray addObject:string];
            }
        }
        [table reloadData];}
}

我对AngularJs很新。问题是我没有在我的表中获取JSON文件数据。在这里,我使用alert来检查数据是否来自JSON文件并且它正在工作,但由于数据没有显示在我的表中,因此以某种方式获取空表。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您需要具有范围变量或控制器变量,并将获取的数据分配给它。以下是控制器变量解决方案:

.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {
        var self = this;
        var promise = tableService.getTable();
        self.tableData = [];
        promise.then(
            function(payload) { 

                data = JSON.stringify(payload.data);
                alert(data);
                self.tableData = data;
            },
            function(errorPayload) {
              $log.error('failure loading movie', errorPayload);
            });

        self.tableEdit = new ngTableParams({
            page: 1,            // show first page
            count: 10           // count per page
        }, {
            total: self.tableData.length, // length of data
            getData: function($defer, params) {
                $defer.resolve(total:tableData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
            }
        });
    })

编辑:以下答案是在OP编辑之前发布的

您指的是data的{​​{1}}属性:

$scope

但你并没有把它分配到任何地方。 您应该更新您的承诺回调:

total:$scope.data.length

答案 1 :(得分:0)

您的代码几乎没有问题,在将数据绑定到视图时应使用$ scope

app.controller('myController', function($scope, myService) {
myService.getJSON().then(function(data){
            $scope.myData =data;
            console.log(data);
 });

});


app.service('myService', function($http) {
  this.getJSON = function() {
    return $http.get('test.json').then(function(data) {
      return data.data.items;
    });
  };
});

<强> DEMO