函数未调用和控制台.log未打印



我的 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。

那不起作用,所以我放了一个日志语句,甚至没有出现。

我做错了什么?

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

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

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

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

(function($) {
})();
})(jQuery); // End of use strict

应该是

(function($) {
})(jQuery);// End of use strict

此外,没有必要在封盖内包裹角度。

我根本不熟悉 Angular,但根据我在这里看到的内容,您的一个标签中可能缺少ng-app="myContent"属性:

W3学校角

最新更新