繁体   English   中英

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

[英]$scope.$apply not working when injecting html with scope variables - AngularJs

我有一个要插入页面的模板

<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]

您需要编译插入的HTML,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM