Javascript not accessible from inside Angular ui-view

前端 未结 7 1640
执念已碎
执念已碎 2021-01-18 17:56

I have a main template (index.html) with an Angular ui-view. Inside this main template I import a bunch of Javascript files. I expect these files to be available to the cont

7条回答
  •  长发绾君心
    2021-01-18 18:47

    I gave up on trying to get the JavaScript to load and the ui-view to recognize it. I ultimately solved this by creating an Angular directive that duplicated the function of the JavaScript.

    Directive:

    angular.module('otr')
        .directive('loginForm', loginFormDirective)
        .directive('registerForm', registerFormDirective)
    ;
    
    function loginFormDirective() {
        return function(scope, element, attrs) {
    
            element.bind('click', function() {
                console.log('element: ', element[0].id);
                console.log('attributes: ', attrs);
    
                $('#' + attrs.loginForm).delay(100).fadeIn(100);
                $("#register-form").fadeOut(100);
                $('#register-form-link').removeClass('active');
                $('#' + element[0].id).addClass('active');
            })
        }
    }
    
    function registerFormDirective() {
        return function(scope, element, attrs) {
    
            element.bind('click', function() {
                $('#' + attrs.registerForm).delay(100).fadeIn(100);
                $("#login-form").fadeOut(100);
                $('#login-form-link').removeClass('active');
                $('#' + element[0].id).addClass('active');
            })
        }
    }
    

    My view now has the following code snippet:

    
    

    Thanks to all of you who took the time to read my question and post answers!

提交回复
热议问题