繁体   English   中英

Angular控制器直接在index.html中

[英]Angular controller directly in index.html

我需要创建一个单页应用程序,而不需要任何其他文件,如app.js或controller.js

我创建了下面的示例,但是我收到错误: Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> JavaScript Angular</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"> var myАpp = angular.module("myАpp", []); myАpp.controller("firstController", function($scope){ $scope.test = "test-to-test"; }); </script> </head> <body ng-app = "myApp"> <div ng-controller = "firstController"> {{test}} </div> </body> </html> 

那么,是否有可能只有一个带有Angular的文件index.html并且仍然有一个控制器?

您需要在<script>标记内包含控制器和模块

 <div ng-app="myАpp"> <div ng-controller="firstController"> <h1>{{test}}</h1> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> <script> var app = angular.module('myАpp', []).controller('firstController', function($scope){ $scope.test = "test-to-test"; }); </script> 

暂无
暂无

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

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