Ⅰ angularjs 全選反選
方法如下:
需要用到AngularJS特性:雙向數據綁定
注意:沒寫一行代碼!這就是ng的優點,bootstrap.css為了布局,JS代碼也只是簡單創建ng模塊和ng控制器。
效果:
代碼如下:
<!DOCTYPE html>
<html lang="en" ng-app="myMole5"><!--3、ng-app="myMole5"啟動ng並調用模塊-->
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/bootstrap.css">
<title>全選/取消全選</title>
</head>
<body>
<div class="container" ng-controller="myCtrl5"><!--4、ng-controller="myCtrl5"啟用控制器-->
<h2>全選和取消全選</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>選擇</th>
<th>姓名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>Tom</td>
<td>
<button class="btn btn-danger btn-xs">刪除</button>
</td>
</tr>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>Mary</td>
<td>
<button class="btn btn-danger btn-xs">刪除</button>
</td>
</tr>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>King</td>
<td>
<button class="btn btn-danger btn-xs">刪除</button>
</td>
</tr>
</tbody>
</table>
<input type="checkbox" ng-model="selectAll">
<span ng-hide="selectAll">全選</span>
<span ng-show="selectAll">取消全選</span>
</div>
<script src="js/angular.js"></script><!--1、引入angularJS-->
<script>
//2、創建自定義模塊和控制器
angular.mole('myMole5', ['ng']).
controller('myCtrl5', function($scope){
});
</script>
</body>
</html>
ps:AngularJs 簡單實現全選,多選操作。
Ⅱ AngularJs中的控制器到底有多少個參數
取決於你注入了幾個參數,一般用法如下:
var app = angular.mole('app', []);
app.controller(function($scope, $http){
//那這個時候就只有這兩個參數,這個東東在angular里叫做依賴注入。並不是默認行為,二是需要你來自己制定的,所以是多少個,就看你自己怎麼用了
});
Ⅲ 如何用angularjs製作一個完整的表格
首先,表格採用的是BootStrap樣式編輯的,主要使用的是angularjs,為了方便也有jQuery的方法,在測試時需自行引入bootstrap,angularjs和jq的文件。
整體代碼預覽:
HTML:
<!DOCTYPE html>
<html lang="en" ng-app="myMole">
<head>
//需要自行引入BOOTStrap,angularjs和jQuery的js,css文件
<style>
.pagination .num{
font-size:22px;color:red;
}
.text{
margin:0 auto;
border:1px solid #ccc;
width:100%;
max-width:200px;
}
</style>
<title>歡迎</title>
</head>
<body ng-controller="myCtrl">
<div class="block">
<div class="navbar navbar-inner block-header">
<div class="muted pull-left">{{kaohzbTitle}}</div>
</div>
<div class="span12" style="float:left;">
<div class="table-toolbar">
<button style="margin-left: 5px;" id="refresh" ng-click="refresh()"
class="btn btn-success">
<i class=" icon-refresh icon-white"></i> 刷新
</button>
<button ng-disabled="isdisabled" class="btn" ng-class="{'btn-info':isInfo}" id="savekaohzb"
ng-click="save()">
<i class="icon-edit icon-white"></i> 保存
</button>
</div>
</div>
<div class="row-fluid">
<div class="span6"></div>
<table class="table table-striped table-bordered table-hover"
id="example" style="margin-top:10px;">
<thead>
<tr>
<th style="width: 20px;" rowspan="2">全選 <br><input type="checkbox" ng-model="selectAll"></th>
<th style="text-align: center; width: 50px;vertical-align: middle" rowspan="2">序號</th>
<th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">名稱</th>
<th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">日期</th>
<th style="text-align: center; width: 150px;" colspan="3">比賽隊伍(紅)</th>
<th style="text-align: center; width: 150px;" colspan="3">比賽隊伍(藍)</th>
<th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">比分</th>
<th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">說明</th>
<th style="text-align: center; width: 150px;vertical-align: middle" rowspan="2">玩家支持隊伍</th>
</tr>
<tr>
<th style="text-align: center; width: 80px;">第一場</th>
<th style="text-align: center; width: 80px;">第二場</th>
<th style="text-align: center; width: 80px;">說明</th>
<th style="text-align: center; width: 80px;">第一場</th>
<th style="text-align: center; width: 80px;">第二場</th>
<th style="text-align: center; width: 80px;">說明</th>
</tr>
</thead>
<tbody ng-click="fun()" id="page" ng-show="isshow">
<!--track by tb.id-->
<tr ng-repeat="tb in saveDate"><!-- 只用angularjs實現點擊一行就選中暫時無法實現 -->
<td style="width: 20px;"><input type="checkbox" ng-checked="selectAll"></td>
<td style="text-align:center;">{{tb.id}}</td>
<td style="text-align:center;">{{tb.zbname}}</td>
<td style="text-align:center;">{{tb.zbtime}}</td>
<td style="text-align:center;">{{tb.zbrul1}}</td>
<td style="text-align:center;">{{tb.zbrul2}}</td>
<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td>
<td style="text-align:center;">{{tb.zbrul2}}</td>
<td style="text-align:center;">{{tb.zbrul1}}</td>
<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td><!-- 2016.1.19通過可編譯的div來代替輸入框 -->
<td style="text-align:center;">{{tb.score}}</td>
<td style="text-align:center;"><div class="text" contenteditable="true" ng-model="tb.por"></div></td>
<td>
<select name="" id="" ng-change="changetype(adds)" ng-model="adds" style="text-align:center;width:100%;min-width:80px;margin-bottom:0">
<option value="" ng-show="isShow">{{tb.type}}</option>
<option value="支持紅方">支持紅方</option>
<option value="支持藍方">支持藍方</option>
<option value="雙方相同">雙方相同</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
<div class="pagination">
<ul style="float:right">
<li id="previous"><a href="">上一頁</a></li>
<li><!--用於頁標的顯示 -->
<ul id="page_num_all">
</ul>
</li>
<li id="next"><a href="" style="border:1px solid #ddd;float:right">下一頁</a></li>
</ul>
<span>
當前為第<span class="num" id="current_page"></span>頁,總共<span class="num" id="page_all"></span>頁
</span>
<span>您當前對select的操作值為:</span>{{typename}}
</div>
<!-- END FORM-->
</div>
</body>
js代碼:
<script>
angular.mole("myMole",[]).controller('myCtrl', function($scope) {
$scope.kaohzbTitle = "考核指標維護";
$scope.search = new Object();
$scope.isdisabled=false;
$scope.isInfo=false;
$scope.saveDate="";//用於保存得到的原始數據
// $http.post請求表格數據
// 模仿請求得到的數據
var datalist=[{
id:1,zbname:"中亞賽區比賽",zbtime:"2015-12-03",zbrul1:"勝利",zbrul2:"失敗",por:"請輸入說明內容",score:"2:1",type:"支持紅方"},{
id:2,zbname:"日韓賽區比賽",zbtime:"2015-11-11",zbrul1:"勝利",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"支持藍方"},{
id:3,zbname:"歐美賽區比賽",zbtime:"2015-3-03",zbrul1:"失敗",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"雙方相同"},{
id:4,zbname:"中東賽區比賽",zbtime:"2016-1-05",zbrul1:"勝利",zbrul2:"失敗",por:"請輸入說明內容",score:"2:1",type:"支持藍方"},{
id:5,zbname:"北京賽區比賽",zbtime:"2014-12-23",zbrul1:"失敗",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"雙方相同"},{
id:6,zbname:"韓國賽區比賽",zbtime:"2015-11-01",zbrul1:"失敗",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"雙方相同"},{
id:7,zbname:"日本賽區比賽",zbtime:"2011-1-23",zbrul1:"勝利",zbrul2:"失敗",por:"請輸入說明內容",score:"2:1",type:"支持紅方"},{
id:8,zbname:"中亞賽區比賽",zbtime:"2013-12-15",zbrul1:"失敗",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"支持藍方"},{
id:9,zbname:"中亞賽區比賽",zbtime:"2015-10-17",zbrul1:"失敗",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"支持紅方"},{
id:10,zbname:"中亞賽區比賽",zbtime:"2015-11-21",zbrul1:"勝利",zbrul2:"勝利",por:"請輸入說明內容",score:"2:1",type:"支持藍方"},{
id:11,zbname:"中亞賽區比賽",zbtime:"2015-2-02",zbrul1:"失敗",zbrul2:"失敗",por:"請輸入說明內容",score:"2:1",type:"支持紅方"},{
id:12,zbname:"中亞賽區比賽",zbtime:"2015-2-05",zbrul1:"勝利",zbrul2:"失敗",por:"請輸入說明內容",score:"2:1",type:"雙方相同"}];
$scope.fun=function(){
var e=window.event||arguments[0];
var src=e.srcElement||e.target;
if(src.nodeName=="TD"){
var par=src.parentNode;
var sd=par.getElementsByTagName("td")[0];
if(sd.firstChild.checked==true){
sd.firstChild.checked=false;
}else{
$("tr td").attr("checked",false);
sd.firstChild.checked=true;
}
}
}
$scope.refresh=function(){//點擊刷新按鈕顯示表格
$scope.saveDate=datalist;
// console.log("結束賦予數據");
$scope.$watch("saveDate",function(){//2016.1.20監聽列表生成數據,當發生變化時執行刷新列表
table_page();
$scope.isshow=true;
});
}
$scope.save=function(){//頁面提交按鈕
console.log("准備保存");
console.log($scope.saveDate);//只要數據改變,自動保存到原始數據列表中
}
//表格分頁功能
function table_page(){
var show_page=5;//每頁顯示的條數
var page_all=$("#page").children().size();//總條數
var current_page=1;//當前頁
// console.log(page_all);
var page_num=Math.ceil(page_all/show_page);//總頁數
var current_num=0;//用於生成頁標的計數器
var li="";//頁標元素
while(page_num>current_num){//循環生成頁標元素
li+='<li class="page_num"><a href="javasctip:(0)">'+(current_num+1)+'</a></li>';
current_num++;
}
$("#page_num_all").html(li);//添加頁標到頁面
$('#page tr').css('display', 'none');//設置隱藏
$('#page tr').slice(0, show_page).css('display', '');//設置顯示
$("#current_page").html(""+current_page+"");//顯示當前頁
$("#page_all").html(""+page_num+"");//顯示總頁數
$("#previous").click(function(){//上一頁
var new_page=parseInt($("#current_page").text())-1;
if(new_page>0){
$("#current_page").html(""+new_page+"");
tab_page(new_page);
}
});
$("#next").click(function(){//下一頁
var new_page=parseInt($("#current_page").text())+1;//當前頁標
if(new_page<=page_num){//判斷是否為最後或第一頁
$("#current_page").html(""+new_page+"");
tab_page(new_page);
}
});
$(".page_num").click(function(){//頁標跳轉
var new_page=parseInt($(this).text());
tab_page(new_page);
});
function tab_page(index){//切換對應頁標的頁面
var start=(index-1)*show_page;//開始截取的頁標
var end=start+show_page;//截取個數
$('#page').children().css('display', 'none').slice(start, end).css('display', '');
current_page=index;
$("#current_page").html(""+current_page+"");
}
}
}).directive('contenteditable', function() {//自定義ngModel的屬性可以用在div等其他元素中
return {
restrict: 'A', // 作為屬性使用
require: '?ngModel', // 此指令所代替的函數
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {
return;
} // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html(ngModel.$viewValue || '');
};
// Listen for change events to enable binding
element.on('blur keyup change', function() {
scope.$apply(readViewText);
});
// No need to initialize, AngularJS will initialize the text based on ng-model attribute
// Write data to the model
function readViewText() {
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if (attrs.stripBr && html === '<br>') {
html = '';
}
ngModel.$setViewValue(html);
}
}
};
})
</script>
Ⅳ angularJS關於依賴和模塊與amd/cmd的區別,分享下結合使用示例
雙向綁定,可測試性的代碼結構,模型視圖分離的一個前端MV*框架
其中angular也提供了模型的概念和依賴管理,不過這個依賴都是要在js對象都已經定義的前提下,沒有像amd/cmd提供按需載入。
我個人比較喜歡cmd(seajs),它對頂級作用域window的使用約束較多,全局對象和方法少,缺點就是很多原生庫,都需要手工wrap下。
angular定義的controller一般都是全局的,我想用seajs來管理angular的代碼和依賴,下面是一起使用的示例,有類似需求的童鞋可以參考下:
//fileng_mole2.js
define(function(require){
varLog=require('log');
return{
init:function(){
Log.w('Loadangularmole:m2');
varag=window.angular;
if(!ag){
Log.w('Errorwhenloadangularmole:m2:noangular');
return;
}
varm2=ag.mole('m2',[]);
m2.filter('greet',function(){
returnfunction(name){
return'Hello,'+name+'!';
};
});
}
};
});
//fileng_mole1.js
define(function(require){
require('mole/demo/ng_mole2').init();
varLog=require('log');
return{
init:function(){
Log.w('Loadangularmole:m1');
varag=window.angular;
if(!ag){
Log.w('Errorwhenloadangularmole:m1:noangular');
return;
}
varm1=ag.mole('m1',['m2']);
m1.directive('testDateFormat',function(){
returnfunction(scope,el,attrs,ctrl){
varformat='yyyy-MM-dd';
varupdateTime=function(){
el.text(newDate().format(format));
};
//watchscope.formatinctrl
scope.$watch('format',function(value){
format=value;
updateTime();
});
updateTime();
}
});
}
};
});
//filedemo/ng1.js
//初始化頁面
define(function(require){
varLog=require('log');
require('mole/demo/ng_mole1').init();
varagAdaptor=require('x/x.ex.angular');
return{
initPage:function(from,pageInfo,params){
varTestCtrl=function($scope){
$scope.format='yyyy/MM/dd';
};
window.TestCtrl=TestCtrl;
agAdaptor.init(['m1'],'TestCtrl','ngContext');
},
mp:''
};
});
//filex/x.ex.angular.js
//angularbootstrap適配——在bootstrap之前動態修改下dom
define(function(require){
var$=require('jquery');
varLog=require('log');
return{
init:function(moles,ctrlName,contextId){
if(!window.angular){
Log.w('Noangluardefined!','WARN');
return;
}
var_context=$('#'+contextId);
this.initCtrl(_context,ctrlName);
this.initModel(_context);
this.bootstrapAngular(moles);
},
//把ng-controller補上
initCtrl:function(_context,ctrlName){
if(ctrlName)
_context.attr('ng-controller',ctrlName);
},
//根據name把ng-model補上
initModel:function(_context){
_context.find('[name^=f_]').each(function(){
var_el=$(this);
varname=_el.attr('name');
varmodelName=name.split('_').remove(0).join('.');
_el.attr('ng-model',modelName);
});
},
bootstrapAngular:function(moles){
window.angular.bootstrap(document,moles);
}
};
});
<divclass="m_10">
<h3>Angular——WorkwithSeaJS</h3>
<divid="ngContext">
Dateformat:<inputng-model="format">
<br/>
Currenttimeis:<spantest-date-format=""></span>
</div>
</div>
seajs.use('mole/demo/ng1',function(IPage){
IPage.initPage();
});
Ⅳ angularjs怎麼編寫一個公共的彈出層插件
下面是AngularUI上的例子,有幾點需要注意的地方
不要忘了引用bootstrap.css和ui.bootstrapmole
不要忘了template
<html ng-app="ui.bootstrap.demo">
<head>
<script src="angular.js"></script>
<script src="ui-bootstrap-tpls-0.12.1.min.js"></script>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li ng-repeat="item in items">
<a ng-click="selected.item = item">{{ item }}</a>
</li>
</ul>
Selected: <b>{{ selected.item }}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<button class="btn btn-default" ng-click="open()">Open me!</button>
<button class="btn btn-default" ng-click="open('lg')">Large modal</button>
<button class="btn btn-default" ng-click="open('sm')">Small modal</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
</body>
<script>
angular.mole('ui.bootstrap.demo',['ui.bootstrap'])
.controller('ModalDemoCtrl', function($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function(size) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function() {
return $scope.items;
}
}
});
modalInstance.result.then(function(selectedItem) {
$scope.selected = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
})
.controller('ModalInstanceCtrl', function($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function() {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
});
</script>
</html>
Ⅵ angularjs 模塊化 css怎麼寫
控制器模塊化
指令模塊化
過濾器模塊化
服務模塊化
定義值模塊化
使用模塊工作
第一步:創建一個模塊
在視圖中應用模塊
<!-- use mole --><html ng-app="exampleApp">...</html>第二步:定義值
第三步:定義服務
第四步:定義控制器
var controllerMole = angular.mole("exampleApp.Controllers", []);// function : define a controller named dayCtrl// the controller include two param:// param detail:// param one : name of controller// param two : a factory function// the param $scope of factory function show information to viewcontrollerMole.controller("dayCtrl", function ($scope, days) {// days : use custom service// today is ...$scope.day = days.today;// tomorrow is ...$scope.tomorrow = 7;})將控制器應用於視圖
第五步:定義指令
將指令應用於視圖
第六步:定義過濾器
將過濾器應用於視圖
<!-- 用 | 分開 --><h4 highlight="Saturday">Today is {{ day || "unknow" | dayName }}</h4><h4>Tomorrow is {{ tomorrow || "unknow" | dayName }}</h4>最後,下面是完整的代碼:
文件一:example.html
文件二:services/exampleService.js
文件三:values/exampleValue.js
文件四:directives/exampleDirective.js
文件五:controllers/exampleController.js
文件六:filters/exampleFilter.js
var filterMole = angular.mole("exampleApp.Filters", []);// function : define a fitler named dayNamefilterMole.filter('dayName', function () {var dayNames = ['Sunday', "Monday", 'Tuesday', 'Wednesday', 'Thurday', 'Friday', 'Saturday'];return function (input) {// input is the value of data bindingreturn angular.isNumber(input % 7) ? dayNames[input % 7] : input % 7;};})Ⅶ 如何使用angularjs處理動態菜單
用ng-repeat命令循環輸出數組,數組內的元素隨你定,angularjs是數據雙向綁定的
舉例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-repeat="x in records">{{x}}</h1>
<script>
var app = angular.mole("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = [
"菜鳥教程1",
"菜鳥教程2",
"菜鳥教程3",
"菜鳥教程4",
]
});
</script>
</body>
</html>