$ scope。$ apply在使用范围变量注入html时不起作用 - AngularJs

时间:2014-03-19 10:02:46

标签: javascript angularjs scope html-injections

我有一个模板,我正在尝试注入我的页面

<div ng-repeat="week in weeks" class="main_container">
    <div id="week{{week.weekNumber}}" style="height: 100%">
    ...
    </div>
</div>

模板已正确添加到我的主html页面中,但是当我使用$scope.$apply()重新分析我的html时,它会改变任何内容。

我已检查过断点,范围包含所需的$scope.weeks数组。

这是我的控制器

var PlanningController = function ($scope, $window, $routeParams, $compile) {

var that = this;

this.currentIndex = 1;
this.pageIndex = '0';
this.disallowRightScroll = false;
var carousel;


app.databaseManager.getUserDAO().getUser(
    function (user) {


        if (user) {
            $scope.user = user;
            $scope.weeks = [];
            if ($routeParams.weekIndex) {
                $scope.weekIndex = $routeParams.weekIndex;
            }
            else {
                $scope.weekIndex = PlanningManager.getWeekIndexFromTimeStamp($scope.user.date_start_planning);
            }
            var dayIndex = PlanningManager.getDayIndexFromDate(new Date());


            app.planningManager.getWeeksJson($scope.user, [$scope.weekIndex, $scope.weekIndex + 1, $scope.weekIndex + 2], function (jsonWeeks) {

                $scope.weeks = jsonWeeks;
                var htmlWeekTemplate;
                $.get("modules/planning/partials/weekTemplate.html", function (data) {
                    htmlWeekTemplate = data;
                    htmlWeekTemplate = $compile(htmlWeekTemplate)($scope);
                    var slides = [
                        htmlWeekTemplate
                    ];


                    carousel = new SwipeView('#week-slider', {
                        numberOfPages: slides.length,
                        hastyPageFlip: true
                    });


                    /**
                     * LOAD ALL CHILDREN INTO CAROUSEL
                     */

                    // Load initial data
                    for (var i = 0; i < 3; i++) {
                        var el = document.createElement('div');
                        el.innerHTML = slides[i];
                        carousel.masterPages[i].appendChild(el)
                    }


                    $scope.$apply();
                });

            });
        } else {
            $scope.user = new User();
        }

    }
);


/*    $('.main_container').find('div').each(function(){
 //var innerDivId = $(this).attr('id');
 carousel.masterPages[i].appendChild($(this));
 });*/

};

关于我做错了什么的想法?

更新

如果我尝试$ compile(htmlWeekTemplate)($ scope),这就是我得到的;

[comment, jquery: "2.0.3", constructor: function, init: function, selector: "", toArray: function…]
0: comment
baseURI: null
childNodes: NodeList[0]
data: " ngRepeat: week in weeks "
firstChild: null
jQuery203016117800935171545: undefined
lastChild: null
length: 25
localName: null
namespaceURI: null
nextElementSibling: div.main_container.ng-scope
nextSibling: div.main_container.ng-scope
nodeName: "#comment"
nodeType: 8
nodeValue: " ngRepeat: week in weeks "
ownerDocument: document
parentElement: null
parentNode: document-fragment
prefix: null
previousElementSibling: null
previousSibling: null
textContent: " ngRepeat: week in weeks "
__proto__: Comment
length: 1
__proto__: Object[0]

1 个答案:

答案 0 :(得分:3)

您需要编译插入的HTML,

使用$compile(newHtmlInserted)($scope);