ng-repeat变量到ng-model

时间:2016-05-11 02:51:43

标签: javascript angularjs django angular-ngmodel

我的HTML:

{%verbatim%}
                <tr ng-repeat="rank in sev_ranks">
                      <td>
                        {{rank.id}}
                      </td>

                      <td>
                        <div class="col-md-12" style="height:100px">
                         {{rank.siverity}}<br>
                        <textarea ng-model="rank.ngmodel" class="form-control"></textarea>
                         </div>
                      </td>

                      <td>
                        <div class="col-md-12" style="height:100px">
                           {{rank.criteria}}<br>
                        <textarea class="form-control">{{rank.criteria}}</textarea>
                         </div>
                      </td>
                </tr>
                {%endverbatim%}

Angular脚本:

$scope.sev_ranks = [{
                id: 1,
                siverity: 'A',
                criteria: 'D',
                ngmodel: 'stxtarea1'
        }, {
            id: 2,
            siverity: 'B',
            criteria: 'E',
            ngmodel: 'stxtarea2'
        }, {
            id: 3,
            siverity: 'C',
            criteria: 'F',
            ngmodel: 'stxtarea3'
        }];

        $scope.stxtarea1 = 'A';
        $scope.stxtarea2 = 'B';
        $scope.stxtarea3 = 'C';

我想实际动态更改ng-model的值。例如:ng-model =“stxtarea1”。但根据代码,我只能获得ng-model =“rank.ngmodel”,而不是更改值。我能做些什么?我还将mng模型值放在“{{}}”中。但没有工作。可以帮我吗?提前谢谢。

当我添加ng-model="{{rank.ngmodel}}"时,我得到的角度误差为Syntax Error: Token 'rank.ngmodel' is unexpected, expecting [:] at column 3 of the expression [{{rank.ngmodel}}] starting at [rank.ngmodel}}].

2 个答案:

答案 0 :(得分:1)

根据我的想法,您需要更改角度脚本,如下所示:

$scope.sev_ranks = [{
                id: 1,
                siverity: 'A',
                criteria: 'D',
                ngmodel: $scope.stxtarea1
        }, {
            id: 2,
            siverity: 'B',
            criteria: 'E',
            ngmodel: $scope.stxtarea2
        }, {
            id: 3,
            siverity: 'C',
            criteria: 'F',
            ngmodel: $scope.stxtarea3
        }];

        $scope.stxtarea1 = 'A';
        $scope.stxtarea2 = 'B';
        $scope.stxtarea3 = 'C';

你正在写'stxtarea1' - 所以它正在考虑作为一个字符串。所以你需要把它写成$scope.stxtarea1

答案 1 :(得分:1)

检查代码,它的逻辑正常。

ifstream
int main() {
    double socialSN = 0;
    string memberFName = "";
    string memberMName = "";
    string memberLName = "";
    double memberID = 0;
    char div = ',';

    ifstream infile;
    infile.open ("salary_database2.txt");
    if(!infile) {cout << "Error: File not found or corrupt. "<< endl; return 1;}

    while(infile >> socialSN >> memberLName >> div >> memberMName >> div >> memberFName >> memberID) {
        cout << setprecision(0) << fixed;
        cout << memberFName << "     " << memberMName << "     " << memberLName << endl;
    }
    return 0;
}