在控制器异步调用后添加CSS样式的首选AngularJS方式是什么?
What is the preferred AngularJS way of adding CSS styling after Controller asynchronous call?
我正在用NW.js和AngularJS构建一个应用程序GitHub Link
我的应用程序从本地文件夹检索文件名,并在应用程序中以列表的形式显示这些文件名。我希望列表中的第一个项目看起来与其他项目不同,因为它开始为"选定"按钮/项目。文件数据是异步的。
目前,我将文件数据作为一个服务加载,该服务在控制器中提取文件名。因为文件数据使用异步函数,所以我把它放在async中。控制器内的系列调用。这个异步调用完成后,ng-bind生效,列表显示在我的应用程序中。
我已经尝试添加不同的指令来将所选类添加到第一个项目,但它们都在列表显示在屏幕上之前被调用。
有人可以帮助我了解什么是首选的angularjs方式设置类或css属性的元素后,它已被绑定到数据?
下面是相关代码。要获得完整的项目,请点击上面的GitHub链接。
h2控制器 fileVizApp.controller("fileVizController", function ($scope, configs, consoles, $location) {
var async = require('async');
var filehelper = require('filehelper');
var consoleKeys = [];
for(var key in consoles) {
consoleKeys.push(key);
}
async.each(consoleKeys, function(currConsole, callback) {
filehelper.GetItemList(consoles, currConsole, callback);
var a = 9;
}, function(err) {
if(err) {
return next(err);
}
$scope.headerSrc = "tmpl/header.html";
$scope.configs = configs;
$scope.back = function () {
window.history.back();
};
$scope.getCount = function (n) {
return new Array(n);
}
$scope.isActive = function (route) {
return route === $location.path();
}
$scope.isActivePath = function (route) {
return ($location.path()).indexOf(route) >= 0;
}
$scope.$apply( function () {
$scope.consoles = consoles;
if(consoles.length > 0) {
$scope.currConsoleInd = 0;
if(consoles.length > 1) {
$scope.nextConsoleInd = 1;
$scope.prevConsoleInd = consoles.length - 1;
} else {
$scope.nextConsoleInd = -1;
$scope.prevConsoleInd = -1;
}
}
else {
$scope.nextConsoleInd = -1;
$scope.prevConsoleInd = -1;
}
});
$scope.$broadcast("Consoles_Ready");
});
});
有关HTML <!-- index.html -->
<!DOCTYPE html>
<html data-ng-app="fileVizApp">
<head>
<title>File Visualizer</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/app.css">
<link rel="stylesheet" type="text/css" href="css/sidebar.css">
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
<script type="text/javascript" src="js/lib/angular-route.min.js"></script>
<script type="text/javascript" src="js/lib/jquery.min.js"></script>
<script type="text/javascript" src="js/lib/bootstrap.min.js"></script>
<script type="text/javascript" src="js/lib/ui-bootstrap-custom-tpls-0.13.0.min.js" ></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/service/services.js"></script>
<script type="text/javascript" src="js/controller/controllers.js"></script>
<script type="text/javascript" src="js/router/routes.js"></script>
<script type="text/javascript" src="js/directive/directives.js"></script>
</head>
<body class="container" data-ng-controller="fileVizController" main-directive>
<div data-ng-include src="headerSrc"></div>
<div id="container">
<div data-ng-view=""></div>
</div>
</body>
</html>
<!-- home.html-->
<div class="">
<!-- Sidebar -->
<ym-gamelist/>
<!-- /#sidebar-wrapper -->
</div>
<!-- itemlist.html -->
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<div ng-repeat="thisConsole in consoles">
<div ng-repeat="item in thisConsole.items" button-repeat>
<li>
<a class="itembutton" href="#"><span ng-bind="item"></span></a>
</li>
<li class="divider"></li>
</div>
</div>
</ul>
</div>
指令
fileVizApp.directive('ymGamelist', function() {
return {
restrict: 'AE',
scope: {},
controller: 'fileVizController',
link: function(scope, element, attrs) {
scope.$on('Consoles_Ready', function () {
var newa = 1;
});
},
compile: function (element, attrs) {
return {
pre: function(scope, iElem, iAttrs){
console.log(name + ': pre link => ' + iElem.html());
},
post: function(scope, iElem, iAttrs){
console.log(name + ': post link => ' + iElem.html());
}
}
},
templateUrl: 'tmpl/itemlist.html'
};
});
fileVizApp.directive('buttonRepeat', function($compile) {
return function(scope, element, attrs) {
if(scope.$last) {
scope.$emit('Itemlist_Loaded');
}
};
});
fileVizApp.directive('mainDirective', function() {
return function(scope, element, attrs) {
scope.$on('Itemlist_Loaded', function (event) {
$('.gamebutton').first().addClass('selectedbutton');
});
};
});
fileVizApp.controller("fileVizController", function ($scope, configs, consoles, $location) {
var async = require('async');
var filehelper = require('filehelper');
var consoleKeys = [];
for(var key in consoles) {
consoleKeys.push(key);
}
async.each(consoleKeys, function(currConsole, callback) {
filehelper.GetItemList(consoles, currConsole, callback);
var a = 9;
}, function(err) {
if(err) {
return next(err);
}
$scope.headerSrc = "tmpl/header.html";
$scope.configs = configs;
$scope.back = function () {
window.history.back();
};
$scope.getCount = function (n) {
return new Array(n);
}
$scope.isActive = function (route) {
return route === $location.path();
}
$scope.isActivePath = function (route) {
return ($location.path()).indexOf(route) >= 0;
}
$scope.$apply( function () {
$scope.consoles = consoles;
if(consoles.length > 0) {
$scope.currConsoleInd = 0;
if(consoles.length > 1) {
$scope.nextConsoleInd = 1;
$scope.prevConsoleInd = consoles.length - 1;
} else {
$scope.nextConsoleInd = -1;
$scope.prevConsoleInd = -1;
}
}
else {
$scope.nextConsoleInd = -1;
$scope.prevConsoleInd = -1;
}
});
$scope.$broadcast("Consoles_Ready");
});
});
<!-- index.html -->
<!DOCTYPE html>
<html data-ng-app="fileVizApp">
<head>
<title>File Visualizer</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/app.css">
<link rel="stylesheet" type="text/css" href="css/sidebar.css">
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
<script type="text/javascript" src="js/lib/angular-route.min.js"></script>
<script type="text/javascript" src="js/lib/jquery.min.js"></script>
<script type="text/javascript" src="js/lib/bootstrap.min.js"></script>
<script type="text/javascript" src="js/lib/ui-bootstrap-custom-tpls-0.13.0.min.js" ></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/service/services.js"></script>
<script type="text/javascript" src="js/controller/controllers.js"></script>
<script type="text/javascript" src="js/router/routes.js"></script>
<script type="text/javascript" src="js/directive/directives.js"></script>
</head>
<body class="container" data-ng-controller="fileVizController" main-directive>
<div data-ng-include src="headerSrc"></div>
<div id="container">
<div data-ng-view=""></div>
</div>
</body>
</html>
<!-- home.html-->
<div class="">
<!-- Sidebar -->
<ym-gamelist/>
<!-- /#sidebar-wrapper -->
</div>
<!-- itemlist.html -->
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<div ng-repeat="thisConsole in consoles">
<div ng-repeat="item in thisConsole.items" button-repeat>
<li>
<a class="itembutton" href="#"><span ng-bind="item"></span></a>
</li>
<li class="divider"></li>
</div>
</div>
</ul>
</div>
fileVizApp.directive('ymGamelist', function() {
return {
restrict: 'AE',
scope: {},
controller: 'fileVizController',
link: function(scope, element, attrs) {
scope.$on('Consoles_Ready', function () {
var newa = 1;
});
},
compile: function (element, attrs) {
return {
pre: function(scope, iElem, iAttrs){
console.log(name + ': pre link => ' + iElem.html());
},
post: function(scope, iElem, iAttrs){
console.log(name + ': post link => ' + iElem.html());
}
}
},
templateUrl: 'tmpl/itemlist.html'
};
});
fileVizApp.directive('buttonRepeat', function($compile) {
return function(scope, element, attrs) {
if(scope.$last) {
scope.$emit('Itemlist_Loaded');
}
};
});
fileVizApp.directive('mainDirective', function() {
return function(scope, element, attrs) {
scope.$on('Itemlist_Loaded', function (event) {
$('.gamebutton').first().addClass('selectedbutton');
});
};
});
使用ng-repeat
和ng-class
内部可用的$first
变量来完成此操作。像这样
<div ng-repeat="item in thisConsole.items" button-repeat>
<li>
<a class="itembutton" href="#" ng-class={'selectedbutton':$first}><span ng-bind="item"></span></a>
</li>
<li class="divider"></li>
</div>
相关文章:
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- angularjs路线过渡如何以交互方式设置动画
- Angularjs:如何让ui选择只有一种方式的模型竞标
- 在AngularJS中使用多个主题的最佳方式是什么
- AngularJS:使用指令的最佳方式
- 如何在angularjs中以正确的方式在指令中使用ng repeat
- 限制与AngularJS状态匹配的动态URL参数的最佳方式
- 如何使用Angularjs以PHP方式提交表单
- 使用AngularJS应用程序为移动设备(iOS、Android等)构建本地应用程序的最佳方式
- AngularJS:以编程方式将ngif添加到指令中的最佳实践是什么?
- AngularJS中使用ajax获取数据的最佳方式是什么
- 读取$localstorage的非AngularJS方式
- 使用AngularJS进行逻辑分离的最佳方式是什么
- 以这种方式将谷歌图表与AngularJS集成可靠吗
- 以编程方式更改AngularJS输入[date]
- 将JSON从PHP控制器传递到AngularJS控制器的最佳方式
- 绑定窗口滚动AngularJS方式
- AngularJS方式:通过通知功能扩展控制器/视图
- 在视图页面加载执行控制器功能,angularjs方式
- 在控制器异步调用后添加CSS样式的首选AngularJS方式是什么?