Angular - 如何在$ compile中使用$ scope?

时间:2015-11-21 16:50:00

标签: javascript angularjs

我正在尝试将我的范围用于编译....我希望每次单击一个按钮时,都会创建一个包含圆数的div。

main.controller('fightController', function($scope, $http, Services, $compile) {

    $scope.doAFight = function(playerHab, monsterHab) {
        Services.getFight(playerHab, monsterHab)
            .success(function(data){
                $scope.fight = data;
                $scope.round = 0;
            });
    };

    $scope.addRound = function() {
        angular.element(document.getElementById('boiteCombat')).append($compile("
            <div class=rondeCombat>
                <div class=numRonde> Ronde " + $scope.round + " </div>
            </div>
            ")($scope));
    };

});

它只是给我一个错误,我正在尝试做的是非法......

1 个答案:

答案 0 :(得分:3)

您希望操纵数据并让视图绑定生效,而不是自己操纵视图。使用ngRepeat并添加到数组以获取更多行:

$scope.rounds = []; //lets this be the array we iterate over

$scope.addRound = function() {
    $scope.rounds.push({round: $scope.round});
});

HTML:

<div id="boiteCombat">
    <div class="rondeCombat" ng-repeat="round in rounds">
        <div class=numRonde> Ronde {{round.round}} </div>
    </div>
</div>