【问题标题】:Function not being called and console.log not printing未调用函数且未打印 console.log
【发布时间】:2018-01-11 17:42:55
【问题描述】:

我的 Angular 函数没有被调用,它应该是最简单的。

在函数内部,我有一个不打印的console.log() 语句。

我之前已经复制了这段代码(减去 jQuery)所以也许这就是问题所在??

这是 .js 文件:

(function($) {
"use strict"; // Start of use strict

// jQuery for page scrolling feature - requires jQuery Easing plugin
$(document).on('click', 'a.page-scroll', function(event) {
    var $anchor = $(this);
    $('html, body').stop().animate({
        scrollTop: ($($anchor.attr('href')).offset().top - 50)
    }, 1250, 'easeInOutExpo');
    event.preventDefault();
});

// Highlight the top nav as scrolling occurs
$('body').scrollspy({
    target: '.navbar-fixed-top',
    offset: 100
});

// Closes the Responsive Menu on Menu Item Click
$('.navbar-collapse ul li a').click(function() {
    $('.navbar-toggle:visible').click();
});

// Offset for Main Navigation
$('#mainNav').affix({
    offset: {
        top: 50
    }
})

var app = angular.module('myContent',[]);

app.controller('ContentController',function($scope) {

    $scope.XG = function(){
        $scope.hideCCAD = true;
        $scope.hideMATE = true;
        $scope.hideWGA = true;
        $scope.hideXG = true;
        console.log("hello")
    };


});

})();

})(jQuery); // End of use strict

这是精简的 HTML:

<section id="features" class="features" ng-
 controller="ContentController">
    <div class="container">
        <div class="row">
            <div class="container-fluid">
                <div class="containerX">
                    <div class="column column-one">
                        <ul>
                            <button ng-click="CCAD()" 
                             type="button">CCAD</button>
                            <button ng-click="XG()" 
                             type="button">XG</button>
                            <button ng-click="MATE()" 
                             type="button">MATE</button>
                            <button ng-click="WGA()" 
                            type="button">WGA</button>
                        </ul>
                    </div>
               ....

所以当我点击按钮 XG 时,我希望它隐藏某些 div。

这不起作用,所以我输入了一个日志语句,甚至没有显示出来。

我做错了什么?

【问题讨论】:

  • 你有错误吗?
  • 这里说清楚。问题与console.log()无关,问题是你的函数没有被调用。
  • 在显示的 HTML 上方是否有 ng-app 指令?
  • @sheplu 不,我不相信我有错误
  • @ScottMarcus 哦,好吧!我想我的函数正在被调用。我认为它会在单击按钮时被调用?

标签: javascript jquery angularjs logging


【解决方案1】:

删除所有代码,您可以看到问题:

(function($) {

})();

})(jQuery); // End of use strict

应该是

(function($) {

})(jQuery);// End of use strict

此外,没有必要在闭包内包裹 angular。

【讨论】:

    【解决方案2】:

    <section id="features" class="features" ng- controller="ContentController">

    ng-controller 之间有一个空格。以下应该有效:

    &lt;section id="features" class="features" ng-controller="ContentController"&gt;

    【讨论】:

    • 这是由于stackoverflow上的格式,在我的HTML代码中没有空格
    【解决方案3】:

    我对 Angular 一点也不熟悉,但根据我在这里看到的情况,您的某个标签中可能缺少 ng-app="myContent" 属性:

    W3 school angular

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多