简体   繁体   English

获取动态HTML / JQuery以与AngularJS一起使用

[英]Get Dynamic HTML/JQuery to work with AngularJS

I have a web page that uses a directive called custom-html which loads a HTML web url into the page (allowing for sub-templates etc). 我有一个使用名为custom-html的指令的网页,该指令将HTML Web URL加载到页面中(允许子模板等)。 I have this system configured so that it registers properly with $scope but I seem to be having issues to get JQuery listeners to work with it. 我已经配置了这个系统,以便它可以正确地向$scope注册,但是我似乎在让JQuery侦听器使用它方面遇到问题。

For instance I have the following at the bottom of one of my templates (where the custom-html tag is used above this point) 例如,在我的模板之一的底部,有以下内容(此处使用custom-html标记)

$(function() {
    $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
});

The datepicker never works in the sub-templates I am including via the custom-html directive though. datepicker永远不会在我通过custom-html指令包含的子模板中起作用。 Does anyone have an idea on how I can remedy this? 有谁知道我该如何解决? Below is the directive I am using: 以下是我正在使用的指令:

backendApp.directive('customHtml', function($compile, $http){
    return {
        link: function($scope, element, attrs) {
            $http.get(attrs['url']).then(function (result) {
                element.html(result.data);
                element.replaceWith($compile(element.contents())($scope));
            });
        }
    }
});

Using JQuery 1.11.1 and AngularJS 1.2.22 at the moment. 目前正在使用JQuery 1.11.1和AngularJS 1.2.22。 Thanks. 谢谢。

EDIT: My apologies, let me clarify the issue that I am having is that when i click on the .datepicker fields that are being inserted via the custom-html directive that it's not working. 编辑:抱歉,让我澄清一下我遇到的问题是,当我单击通过custom-html指令插入的.datepicker字段时,它不起作用。 Eg it's not opening up the JQuery datepicker as it should be when I click on the input field. 例如,当我单击input字段时,它没有打开JQuery datepicker。 When I do this with the regular HTML (not the custom-html ) it works just fine. 当我使用常规HTML(而不是custom-html )执行此操作时,效果很好。

The problem is that you need to run the datepicker() init AFTER the element gets compiled. 问题是您需要在元素被编译之后运行datepicker()初始化。 So you should do it after you replace the actual html. 因此,您应该在替换实际的html之后执行此操作。 The code you have above runs one time on page load (or wherever it is) and won't create datepickers for html elements created AFTER that point. 您上面的代码在页面加载时(或在任何位置)运行一次,并且不会为之后创建的html元素创建日期选择器。

backendApp.directive('customHtml', function($compile, $http){
return {
    link: function($scope, element, attrs) {
        $http.get(attrs['url']).then(function (result) {
            element.html(result.data);
            element.replaceWith($compile(element.contents())($scope));
            // DO DATEPICKER INIT HERE ON NEW ELEMENT
        });
    }
}
});

The better way to do it is a datepicker directive so you know the datepicker element has been compiled before you init it (in the link function) 更好的方法是使用datepicker指令,这样您就可以在初始化datepicker元素之前知道它已被编译(在link函数中)

backendApp.directive('myDatepicker', function($compile, $http){
return {
link: function($scope, element, attrs) {
    // ONLY JOB IS INITING DATE PICKER HERE
}
}
});

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

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