如何正确设置AngularJS文件
How to properly setup AngularJS files
我熟悉设置angularjs
项目的标准方法,我正在尝试做的是根据页面为不同的控制器和指令设置单独的文件。有关更好的解释,请参见下文。
www /
app.js
index.html
login /
loginDirective.js
loginPage.html
这是我的应用程序.js文件
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
(function () {
'use strict';
angular.module('app', ['ionic']);
})();
var app=angular.module('app');
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
这是我的登录指令.js
(function () {
'use strict';
var app=angular.module('app');
app.config(function($stateProvider) {
$stateProvider
.state('login', {
url: '/login',
template: '<loginDirective></loginDirective>'
})
})
app.directive('loginDirective', loginDirective);
function loginDirective() {
var directive = {
restrict : 'EA',
templateUrl : 'loginPage.html',
controller : loginController,
controllerAs : 'lg',
bindToController : true
};
return directive;
}
function loginController() {
var lg = this;
lg.test = 'this is a test';
console.log('RETURN = %s', 'test');
}
})();
这是我的索引.html
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="login/loginDirective.js"></script>
</head>
<body ng-app="app" animation="slide-left-right-ios7">
<div>
<div>
<ion-nav-bar class="bar-dark">
<ion-nav-title>Sample APP</ion-nav-title>
<ion-nav-back-button class="button-icon icon ion-ios-arrow-back">Back</ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view ></ion-nav-view>
</div>s
</div>
</body></html>
最后但并非最不重要的登录页面.html
<html>
<head>
</head>
<body>
<div login-directive></div>
<ion-view view-title="Login" name="login-view" class="scroll-bg" hide-nav-bar="true">
<ion-content class="padding">
<div align="center" class="imagecontent">
<div style="text-align: center">
<img ng-src="img/logos@2x.png" width="250px">
</div>
</div>
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="Username" style="color: #ffffff" ng-model="data.username">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" style="color: #ffffff" ng-model="data.password">
</label>
</div >
<div class="" >
<div class="">
<button class=" button button-block button-dark" ng-click="login(data)">LOG IN</button>
</div>
</div>
</ion-content>
<div style="position: absolute; bottom: 10%; width: 100%">
<div style="text-align: center">
<img ng-src="img/FNC_Logo.png" width="150px">
</div>
</div>
<ion-footer-bar align-title="right" class="footer-bg">
<div class="col text-right" ng-click="doSomething()">
<button class="button footerbtn-bg" ></button>
</div>
</ion-footer-bar>
</ion-view>
</body>
</html>
我做了什么错误,导致我的登录页面.html不显示?
我可能是错的,因为我是 AngularJS 的新手,但如果我是你,我首先会尝试改变:
templateUrl : 'loginPage.html'
到:
templateUrl : 'login/loginPage.html'
我得出了这样的结论,因为您在index.html中包含loginPage.js文件,因此它会尝试在www目录中查找loginPage.html或在调用它的目录中。我今天遇到了像你这样的案例,但有图像。
您的状态定义有问题:
$stateProvider
.state('login', {
url: '/login',
//This should be kebab-case
template: '<login-directive></login-directive>'
//NOT camelCase
//template: '<loginDirective></loginDirective>'
})
})
但是我不确定为什么当您可以使用控制器定义状态时,为什么要对该状态使用指令:
.state('login', {
url: '/login',
templateUrl : 'loginPage.html',
controller : loginController,
controllerAs : 'lg'
});
尝试将<div login-directive></div>
移动到离子视图中。
还要检查控制台以查看是否实际输出console.log('RETURN = %s', 'test');
。至少你会知道状态是否处于活动状态。
就像@georgeawg说的,使用指令作为模板使这过于复杂。你为什么在这个例子中这样做是我无法理解的。此外,您的$state配置应位于您的 app.js 文件中。
相关文章:
- 如何包含特定于每个视图angularjs的javascript文件
- 点击Angularjs中的按钮上传文件
- 将外部文件与AngularJs集成
- AngularJS-如何列出给定文件夹中的文件名
- 从AngularJS中的另一个文件中的控制器访问服务
- 通过AngularJS promise下载一个文件
- 将AngularJS控制器拆分为多个文件
- 正在分析AngularJS中的JSONP文件
- 使用angularjs和node.js时的Web应用程序文件夹结构
- 如何angularjs将日志写入文件
- 使用正则表达式angularjs验证文件路径
- AngularJS分为多个文件-只有最后一个文件有效
- 通过一个javascript文件引导AngularJS javascript文件
- AngularJS可以在没有任何服务器端技术的情况下将文件保存/写入服务器
- 错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“文件”:AngularJS SPA
- 从另一个文件 AngularJS 中的另一个控制器调用函数
- django提供静态文件/angularJS应用程序
- 如何制作一个单独的html文件AngularJS应用程序
- 从JSON文件AngularJS注入的编译指令
- 从js文件(angularjs)调用web服务