Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 渲染后连接jQuery插件_Angularjs_Fancybox_Angularjs Directive - Fatal编程技术网

Angularjs 渲染后连接jQuery插件

Angularjs 渲染后连接jQuery插件,angularjs,fancybox,angularjs-directive,Angularjs,Fancybox,Angularjs Directive,我创建了一个指令,ngAfterRender,我正试图用它连接fancybox: 模板 <div ng-after-render="wireUpFancyBox($event)" ng-bind-html="Content"></div> 控制器 $scope.wireUpFancyBox = function ($element) { $element.find('a').fancybox($.extend(true, {}, fancybox

我创建了一个指令,
ngAfterRender
,我正试图用它连接fancybox:

模板

<div ng-after-render="wireUpFancyBox($event)" ng-bind-html="Content"></div>
控制器

    $scope.wireUpFancyBox = function ($element) {
        $element.find('a').fancybox($.extend(true, {}, fancyboxOptions, {
            scrolling: 'auto',
            type: 'inline'
        }));
    };
不幸的是,当HTML
内容
绑定更改时,不会调用
wireUpFancyBox()
方法


我能在这里做什么?

如果您想在每次
ng bind
更改时调用
wireUpFancyBox
,那么您需要这样的东西

myApp.directive('ngAfterRender', ['$timeout', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return scope.$eval(attrs.ngBindHtml);
            },
            function (value) { 
               $timeout(function () {   
                  scope.$eval(attrs.ngAfterRender, { $element: element });
               });
            });
        }
    };
}]);
在这里,我们对经过评估的
ngbindthml
表达式进行监视,然后在进行更改时启动
eval


如果您想在每次
ng bind
更改时调用
wireUpFancyBox
,那么您需要这样的东西

myApp.directive('ngAfterRender', ['$timeout', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return scope.$eval(attrs.ngBindHtml);
            },
            function (value) { 
               $timeout(function () {   
                  scope.$eval(attrs.ngAfterRender, { $element: element });
               });
            });
        }
    };
}]);
在这里,我们对经过评估的
ngbindthml
表达式进行监视,然后在进行更改时启动
eval


试试
$timout(函数(){},0)@Fourth:这没什么区别。它会在页面的第一次呈现时绑定,但在随后修改
内容
值时,它不会被调用。您可以对该问题进行处理吗?我的想法是,您可以将范围监视绑定到attrs.ngBindHtml,然后在它更改时重新评估。在我看来,问题在于您的指令只会在第一次渲染时运行,而不会注意内容的变化,并且无法对这种情况做出反应@Fourth:这没什么区别。它会在页面的第一次呈现时绑定,但在随后修改
内容
值时,它不会被调用。您可以对该问题进行处理吗?我的想法是,您可以将范围监视绑定到attrs.ngBindHtml,然后在它更改时重新评估。在我看来,问题在于,您的指令只会在第一次渲染时运行,而不会注意内容的变化,无法对这种情况做出反应。好奇——为什么是janky?对于meIt的janky来说,感觉很正常(虽然远不是很明显——因此你的评论?),因为不能保证DOM在调用$timeout的结果时已经呈现。对吧?你是有保证的。这是一个奇怪的问题——为什么是janky?对于meIt的janky来说,感觉很正常(虽然远不是很明显——因此你的评论?),因为不能保证DOM在调用$timeout的结果时已经呈现。对吧?你是有保证的。这里有一个