$ scope。$ apply()不更新动态指令

时间:2016-01-09 02:50:03

标签: angularjs dynamic directive

我在点击按钮时动态添加指令。 当我尝试从其链接函数更新动态生成的指令模板时,它不会更新,但我在控制台中获取更新的值。 我的代码是:: MAIN指令::

app.directive("loadingDivStart",["$compile",function($compile){    
        return{
            restrict:"A",
            link:function ($scope, $elem, $attr){
                console.log('me');
                $elem.on('click',function(e){
                   newElement = $compile("<div id='loaderContainer' loading-directive  mywindow='windowProp' my-timer='timer' style='height:100%;'></div>")($scope);
                   $('body').append(newElement); 
                });
            }
        }    
    }])

动态指令

    app.directive("loadingDirective", ["$timeout","$interval", function ($timeout,$interval) {
        return {
            restrict: "A",
            scope: {
                // mytimer: "=",
                windowProp: "=mywindow",
                myTimer: "="
            },
            template: "<div class='loader' style='width:{{windowProp.width}}px;height:{{windowProp.height}}px;'><div class='svgAnimation'>{{fromVal}}</div></div>",
            link: function (scope, elem, attrs) {
                scope.fromVal = '08:00';
                $timeout(function(){
                    scope.fromVal = 'Hello';
                    console.log("Changed");
                }, 5000);
            }
        }

}]);

$ timeout

之后, fromVal 的值未更新

0 个答案:

没有答案