在这里,我使用此示例here进行了angularjs练习。问题在于,即使我通过ng-app
和<script>
标记调用了脚本。编译器没有或无法找到应用程序。我做错了什么?
这是index.html: -
<!DOCTYPE html>
<html ng-app="MyApp" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="author" content="Scripting tutorial" />
<title>Angularjs Responsive Website</title>
<meta name="description" content="This is a tutorial that I did to learn more about angularjs" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="css/StyleSheet.css" rel="stylesheet" />
<script src="js/Scripts/jquery-1.9.1.min.js"></script>
<script src="js/Scripts/angular.min.js"></script>
<script src="js/Scripts/angular-route.min.js"></script>
<script src="js/app.js"></script>
<script src="js/HomeCtrl.js"></script>
<script src="js/ProjectCtrl.js"></script>
<script src="js/ProjectCtrl.js"></script>
<script src="js/AboutCtrl.js"></script>
</head>
<body>
<!--links-->
<header>
<div class="wrap">
<a href="#!"><img class="logo" src="images/sample_seller.gif" /></a>
<nav>
<ul>
<li><a id="workBtn" href="#!/" ng-class="{activeSmall:part == 'projects'}">Our Projects</a></li>
<li><a id="privacyBtn" href="#!/privacy" ng-class="{activeSmall:part == 'privacy'}">Privacy & Terms</a></li>
<li><a id="aboutBtn" href="#!/about" ng-class="{activeSmall:part == 'about'}">About</a></li>
<li style="margin-right:0px"><a id="contactBtn" class="active" href="javascript.void(0)" ng-click="showForm()">Contact Us</a></li>
</ul>
</nav>
</div>
</header>
<!--contact form-->
<div class="paddRow contactRow">
<div class="wrap">
<div class="head">Contact Us</div>
<img class="close" src="images/close-button.jpg" ng-click="closeForm()" />
<form ng-submit="save()" class="contactForm" name="form" ng-hide="loaded">
<input class="input" required="required" type="text" name="name" placeholder="Your Name" ng-model="message.name" />
<input class="input email" required="required" type="email" name="email" value="" placeholder="Your Email" ng-model="message.email" />
<textarea class="textarea" rows="5" required="required" placeholder="Your message" ng-model="message.text"></textarea>
<button class="btn green">Send Message</button>
</form>
<!--contact us form response message-->
<div ng-show="process" style="text-align:center">
<img class="loader" src="images/loading.png" />
</div>
<div ng-show="success"><p>Your message has been sent, Thank You!</p></div>
</div>
</div>
<!--Main Content-->
<div style="position:relative">
<div style="width:100%" ng-view ng-animate="{enter: 'view-enter', leave: 'view-leave'}"></div>
</div>
</body>
</html>
这是app.js: -
'use strict';
var app = angular
.module('MyApp',
[
'ngRoute'
])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/',
{
templateUrl: 'pages/pro-index.html',
controller: HomeCtrl
})
.when('project/:projectId',
{
templateUrl: function (params) {
return 'pages/' + params.projectId + '.html';
},
controller: ProjectCtrl,
activetab: 'projects'
})
.when('/privacy',
{
templateUrl: 'pages/privacy.html',
controller: PrivacyCtrl,
activetab: 'privacy'
})
.when('/about',
{
templateUrl: 'pages/about.html',
controller: AboutCtrl,
activetab: 'about'
})
.otherwise(
{
redirectTo: '/'
});
}])
.run(['$rootScope', '$http', '$browser', '$timeout', '$route', function ($scope, $http, $browser, $timeout, $route) {
$scope.$on("$routeChangeSuccess", function (scope, next, current) {
$scope.part = $route.current.activetab;
});
console.log('My app works!');
//onclick event handlers
$scope.showForm = function () {
$('.contactRow').slideToggle();
};
$scope.closeForm = function () {
$('.contactRow').slideUp();
};
// save the Contact us form
$scope.save = function () {
$scope.loaded = true;
$scope.process = true;
$http.post('sendemail.php', $scope.message).success(function () {
$scope.success = true;
$scope.process = false;
});
};
}]);
app.config(['$locationProvider', function ($location) {
$location.hashPrefix('!');
}
]);
错误是它说无法实例化模块MyApp,原因是: HomeCtrl没有定义......我也定义了它
这是:
(function () {
angular
.module('MyApp')
.controller('HomeCtrl', HomeCtrl);
HomeCtrl.$inject = ['$scope'];
function HomeCtrl($scope) {
$scope.Hello = "Hello";
};
})();
答案 0 :(得分:1)
首先在angularJS代码中声明一个控制器,如下所示:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.myFunc = function(){
//YOUR FUNCTION CODE HERE
}
});
将您的功能放在控制器中,并将它们分配给控制器的 $ scope 。然后在HTML中使用 ng-controller ,如下所示:
<body ng-controller="myCtrl">
Full Name: {{firstName + " " + lastName}}
// RUN FUNCTION LIKE SO
<button ng-click="myFunc()"></button>
</body>
希望这有帮助。
答案 1 :(得分:0)
你们不相信我发现的东西。没有人没有。我的应用程序一直都在工作。从新版本的angularjs开始,我的应用程序无法识别我的控制器是因为......等待它...“SINGLE QUOTES”= '...'
所以你必须在单引号内命名控制器。
所以它会是;
controller: 'HomeCtrl'
你有它。所有那些陷入困境的人现在都有尝试的感觉。
感谢帮助伙伴们!
同样感到羞耻的是发布这个问题。