Javascript 加载视图时运行AngularJS控制器
我有一个angularJS单页应用程序,每次点击都会加载视图 我的登录和索引在一个html中。我已经通过ng show和ng if操纵了它 流程如下所示: Index.html和IndexController.js: 登录页面有一个ng show=“!isLogin”,如果它经过身份验证,则isLogin=true 这样我就可以看到我的主页了。代码是这样的Javascript 加载视图时运行AngularJS控制器,javascript,angularjs,materialize,angularjs-material,Javascript,Angularjs,Materialize,Angularjs Material,我有一个angularJS单页应用程序,每次点击都会加载视图 我的登录和索引在一个html中。我已经通过ng show和ng if操纵了它 流程如下所示: Index.html和IndexController.js: 登录页面有一个ng show=“!isLogin”,如果它经过身份验证,则isLogin=true 这样我就可以看到我的主页了。代码是这样的 <section id="content"> <div ng-view> </div> <
<section id="content">
<div ng-view>
</div>
</section>
然后,我有一个default.html,它包含在'isLogin'变为true后看到的第一个页面,其中包含controller DashboardController.js
var dashboardController = angular.module('DashboardController', []).controller('DashboardController', function ($http, $scope, $sessionStorage,$window,SweetAlert,BillService,$filter) {
(function init() {
$(document).ready(function() {
$('select').material_select();
$('.modal-trigger').leanModal();
$('.modal').modal();
});
$scope.overDue = [];
BillService.getBillbyBusinessId($sessionStorage.currentBusiness.businessId).then(function (response) {
angular.forEach(response.data, function (value) {
if (value.status === "active") {
var date = new Date();
date = $filter('date')(date, "MM-dd-yyyy");
if(value.dueDate < date){
$scope.overDue.push(value);
}
}
});
});
var obj=[];
for(var i=1;i<5;i++){
obj.push({Id: i, Value: "Value "+i});
}
$scope.ItemList=obj;
})();
//
//$scope.showCreateForm = function(){
// $('#modal1').show();
//}
});
dashboardController.directive("select2", function ($timeout, $parse) {
return {
restrict: 'AC',
require: 'ngModel',
link: function (scope, element, attrs) {
$timeout(function () {
element.select2();
element.select2Initialized = true;
});
var refreshSelect = function () {
if (!element.select2Initialized) return;
$timeout(function () {
element.trigger('change');
});
};
var recreateSelect = function () {
if (!element.select2Initialized) return;
$timeout(function () {
element.select2('destroy');
element.select2();
});
};
scope.$watch(attrs.ngModel, refreshSelect);
if (attrs.ngOptions) {
var list = attrs.ngOptions.match(/ in ([^ ]*)/)[1];
// watch for option list change
scope.$watch(list, recreateSelect);
}
if (attrs.ngDisabled) {
scope.$watch(attrs.ngDisabled, refreshSelect);
}
}
};
});
但问题是,除非我点击导航按钮,否则控制器不会运行
见“逾期票据”。除非我点击导航栏上的“仪表板”,否则它不会加载
那么在我登录后如何运行我的控制器呢?任何帮助都将不胜感激
这个项目是为了教育目的
多谢各位
编辑:
DashboardController.js
var dashboardController = angular.module('DashboardController', []).controller('DashboardController', function ($http, $scope, $sessionStorage,$window,SweetAlert,BillService,$filter) {
(function init() {
$(document).ready(function() {
$('select').material_select();
$('.modal-trigger').leanModal();
$('.modal').modal();
});
$scope.overDue = [];
BillService.getBillbyBusinessId($sessionStorage.currentBusiness.businessId).then(function (response) {
angular.forEach(response.data, function (value) {
if (value.status === "active") {
var date = new Date();
date = $filter('date')(date, "MM-dd-yyyy");
if(value.dueDate < date){
$scope.overDue.push(value);
}
}
});
});
var obj=[];
for(var i=1;i<5;i++){
obj.push({Id: i, Value: "Value "+i});
}
$scope.ItemList=obj;
})();
//
//$scope.showCreateForm = function(){
// $('#modal1').show();
//}
});
dashboardController.directive("select2", function ($timeout, $parse) {
return {
restrict: 'AC',
require: 'ngModel',
link: function (scope, element, attrs) {
$timeout(function () {
element.select2();
element.select2Initialized = true;
});
var refreshSelect = function () {
if (!element.select2Initialized) return;
$timeout(function () {
element.trigger('change');
});
};
var recreateSelect = function () {
if (!element.select2Initialized) return;
$timeout(function () {
element.select2('destroy');
element.select2();
});
};
scope.$watch(attrs.ngModel, refreshSelect);
if (attrs.ngOptions) {
var list = attrs.ngOptions.match(/ in ([^ ]*)/)[1];
// watch for option list change
scope.$watch(list, recreateSelect);
}
if (attrs.ngDisabled) {
scope.$watch(attrs.ngDisabled, refreshSelect);
}
}
};
});
var dashboardController=angular.module('dashboardController',[]).controller('dashboardController',函数($http、$scope、$sessionStorage、$window、SweetAlert、BillService、$filter){
(函数init(){
$(文档).ready(函数(){
$('select')。材料选择();
$('.modal触发器').leanmodel();
$('.modal').modal();
});
$scope.逾期=[];
BillService.getBillbyBusinessId($sessionStorage.currentBusiness.businessId)。然后(函数(响应){
角度.forEach(响应.数据,函数(值){
如果(value.status==“活动”){
变量日期=新日期();
日期=$filter('date')(日期,“MM dd yyyy”);
如果(value.dueDate<日期){
$scope.逾期.推送(值);
}
}
});
});
var-obj=[];
对于(var i=1;i
{{businessData.companyName}
{{{businessRole.roles}
版权所有©2018保留所有权利。
设计和开发
编辑2:NTP
<body ng-init="change=true">
<button ng-click="change = !change">Click to initialize other controller</button>
<div ng-if="change" ng-controller="MainCtrl">
first controller
<div ng-if="!change" ng-controller="SubCtrl">
second controller
</div>
</div>
</body>
单击以初始化其他控制器
第一控制器
第二控制器
您可以尝试以下操作,因为ngIf指令在isLogin为true时重新创建仪表板部分,所以仪表板控制器将在isLogin为true时运行
<div id="dashboard" ng-if="isLogin" ng-controller="DashboardController">
</div>
每个视图都有一个通过路由器或 每当该视图出现在dom中时,就会调用关联的控制器
<div ng-controller="DashboardController" >
</div>
因为如果非loggedin用户尝试打开它,它将显示空白页。相反,添加一个用于登录的公共服务,并调用您的控制器,如果用户未登录,则重新路由回登录。shae
DashboardController.js
代码段请参阅post againhello,我添加了html代码。我尝试过这样做,但没有解决我的问题t、 您能检查一下我应该把它放在哪里吗?@NTPchange ng show to ng if并将您的控制器添加为ng controller=“DashboardController”在html的哪一部分?@Mark为您添加了plunker您有这样的解决方案吗?请参阅原始第二版是的,但它仍然没有加载,而且路径不会更改,因为我的默认控制器是“localhost:8080/#”,不在id=“content”部分但在default.html内部,将加载到路由器中的htmlInstead上的部分html添加到存在的位置。当添加控制器时:'DashboardController',例如:。当('/dashboard/',{templateUrl:default.html,controller:'DashboardController',})。当(“/Main”,{templateUrl:'views/default.html',controller:'DashboardContr
ng-if="isLogin"