1. 程式人生 > >修改ng-repeat遍歷出來的單條資料

修改ng-repeat遍歷出來的單條資料

    var modifyindex;
    $scope.goto= function(title,url,id,name,pmurl) {
        $scope.authid=id;
        var url = url + '?id='+id+'&name='+name+'&pmurl='+pmurl;
        modifyindex = layer.open({
        type : 2,
        title : title,
        content : url,
        end : function() {
        }

    });

這是angularjs的一個controller中的一個函式,裡面用到了layer.open,這是一個彈窗,content:url是要彈出的頁面,並且向另一個頁面傳遞了引數。其實goto函式是寫在ng-click事件中的,這個ng-click在被ng-repeat遍歷出來的html元素上。然後在另一個頁面,也就是url指向的頁面,再在這個頁面寫一個controller,包含函式

    var getData = $scope.getData = function() {
        var url = window.location.href;
        var da = url.split('?');
        var path= da[1].split('='); //id=id&name=name&pmurl=pmurl
        $scope.authid=path[1].split('&')[0];
        $scope.authname=path[2].split('&')[0];
        $scope.authurl=path[3];
    }   
通過這個就能得到從A頁面傳遞過來的引數,比如將<input >的值傳遞到goto函式,然後彈窗到B頁面,通過getData函式解析得到引數,繫結到B頁面的<input>。

這種情況適用於,通過ng-repeat遍歷出很多條資料,然後點選某一條進行修改,因為每一條資料都不一樣,所以傳遞的引數也不一樣。然後在B頁面顯示被點選那條資料的內容,當然在B頁面也會得到這條資料的id,然後修改name等值,根據id去修改資料,儲存到資料庫。