AngularJS 1.6.6无法使用$ setViewValue更新模型值

时间:2017-12-04 02:59:21

标签: javascript angularjs angularjs-directive

问题

无法更新模型;

使用set函数我可以更改$parsers值,并通过管道函数Del,但是$scope.$apply ()函数使用splice来处理数组,但它无法通过管道功能并实现更新模型值。

我尝试使用ngModel.$setViewValue ($scope.images)1.6.6之后执行,但仍然无法解决。

使用的角度版本为<div ng-controller="appController"> <div image-uploads ng-model="files"></div> <p style="display: block; color: red">{{files}}</p> </div>

在线代码online code links,我希望你能帮我看看问题到底在哪里。

查看代码

var app = angular.module('app', []);
    app.controller('appController', function ($scope) {
        $scope.files = '1,2,3,4';
    });

    app.directive('imageUploads', function () {
    return {
        require: '?^ngModel',
        restrict: 'EA',
        template: '<div class="image-upload-box"><p class="image_upload" ng-repeat="image in images track by $index"><button ng-click="set()">setModel</button><button ng-click="del($index)">{{image}}</button></p></div>',
        link: function ($scope, element, attrs, ngModel) {
            ngModel.$formatters.push(function (modelValue) {
                var images = new Array();
                if (!ngModel.$isEmpty(modelValue)) {
                    var values = modelValue.split(",");
                    for (var j = 0; j < values.length; j++) {
                        images.push({
                            'id': values[j]
                        });
                    }
                }
                return images;
            });

            ngModel.$parsers.push(function (viewValue) {
                var s = "";
                for (var j = 0; j < viewValue.length; j++) {
                    if (viewValue[j].id != null) {
                        if (j > 0) {
                            s += ",";
                        }
                        s += viewValue[j].id;
                    }
                }
                return s;
            });

            ngModel.$render = function () {
                $scope.images = ngModel.$viewValue;
            };

            $scope.del = function (i) {
                $scope.images.splice(i, 1);
                ngModel.$setViewValue($scope.images);
            };
            $scope.set = function () {
                console.log('set');
                $scope.images = [{id: 5}, {id: 6}, {id: 7}];
                ngModel.$setViewValue($scope.images);
            }
        }
    };
});

Javascript代码

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "time.h"
#include <Windows.h>
#include <cstdlib>
using namespace std;
class Open {
protected:
    int fileNum = 0;
    int level = 0;
    string VeryEasy[10], Easy[10], Medium[10], Hard[10];
    ifstream fin;

public:
    void promptLevel() {
        cout << "Anytime throughout the game, Enter N for new game, Q to 
quit, and A to see your average score" << endl;
        cout << "Which would you like to play? Enter 1 for Very Easy, 2 for 
Easy, 3 for Medium, and 4 for Hard" << endl;
        cin >> level;
    }
    void setFiles() {
        for (int i = 0; i < 10; i++) {
            VeryEasy[i] = "VeryEasy" + to_string(i) + ".txt";
            Easy[i] = "Easy" + to_string(i) + ".txt";
            Medium[i] = "Medium" + to_string(i) + ".txt";
            Hard[i] = "Hard" + to_string(i) + ".txt";
        }
    }
    void openFile() {
        fileNum = rand() % 10;
        if (level == 1) fin.open(VeryEasy[fileNum]);
        else if (level == 2) fin.open(Easy[fileNum]);
        else if (level == 3) fin.open(Medium[fileNum]);
        else if (level == 4) fin.open(Hard[fileNum]);
        if (!fin.is_open()) {
            cout << "Failure" << endl;
        }
    }
};
class Board : public Open {
private:
    char boardChar[9][9];
public:
    void readBoard() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                fin.get(boardChar[i][j]);
                cout << boardChar[i][j];
            }
            fin.ignore();
            cout << endl;
        }
    }
};
int main() {
    Open O1;
    Board B1;
    O1.setFiles();
    O1.promptLevel();
    B1.openFile();
    B1.readBoard();

    system("Pause");
    return 0;
}

0 个答案:

没有答案