㈠ 請教angularjs 如何 console里直接 執行以下函數
第一個要注意的是這些函數的調用順序:
復制代碼 代碼如下:
// COMPILE PHASE
// levelOne: compile function is called
// levelTwo: compile function is called
// levelThree: compile function is called
// PRE-LINK PHASE
// levelOne: pre link function is called
// levelTwo: pre link function is called
// levelThree: pre link function is called
// POST-LINK PHASE (Notice the reverse order)
// levelThree: post link function is called
// levelTwo: post link function is called
// levelOne: post link function is called
這個例子清晰的顯示出了ng在link之前編譯所有的指令,然後link要又分為了pre-link與post-link階段.
注意下,compile與pre-link的執行順序是依次執行的,但是post-link正好相反.
所以上面已經明確標識出了不同的階段,但是compile與pre-link有什麼區別呢,都是相同的執行順序,為什麼還要分成兩個不同的函數呢?
DOM
為了挖的更深一點,讓我們簡單的修改一下上面的代碼,它也會在各個函數里列印參數列表中的element變數
復制代碼 代碼如下:
var app = angular.mole('plunker', []);
function createDirective(name){
return function(){
return {
restrict: 'E',
compile: function(tElem, tAttrs){
console.log(name + ': compile => ' + tElem.html());
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());
}
}
}
}
}
}
app.directive('levelOne', createDirective('levelOne'));
app.directive('levelTwo', createDirective('levelTwo'));
app.directive('levelThree', createDirective('levelThree'));
注意下console.log里的輸出,除了輸出原始的html標記基本沒別的改變.
這個應該能夠加深我們對於這些函數上下文的理解.
㈡ angularjs 怎麼運行文件
可以向其中添加更多樣式以提升成果的視覺效果——但請注意,本教程中的截圖都採取最基本的外觀設計。
大家可以未來需要編寫的JavaScript代碼置於本文檔的<head>當中或者為其建立獨立文件,但獨立文件仍然需要處於AngularJS腳本之下。
第二步:模塊
現在我們可以為自己的應用程序創建一個模塊:
var app = angular.mole('githubsearch', []);
接下來利用ngApp指令將其添加到<body>標簽當中:
<body ng-app="githubsearch">
第三步:控制器
我們還需要為自己的應用程序准備一套控制器。為了簡化創建流程,我們將只為應用准備一套控制器,這樣我們就不必考慮如何在不同控制器之間進行信息傳遞了:
app.controller('SearchController', function SearchController($scope) { });
第四步:基礎服務
我們需要對自己的GitHub服務進行定義:
app.factory('GitHub', function GitHub($http) { return { }; });
我們將使用app.factory()方法,這樣就能保證返回對象附帶幾個以後將會用到的方法。我們將使用$http服務從GitHub的API中獲取數據。
第五步:搜索庫
我們服務中的第一項方法負責利用GitHub API對庫進行搜索。使用服務非常簡單(這項函數能夠進入由製造函數返回的對象):
㈢ 如何使用angularjs處理動態菜單
angularjs處理動態菜單的實現方法:
1、核心angularjs代碼:
var testImg=angular.mole("appTest",["ms.leafMenu"])
.controller('testCtr',['$scope',function($scope){
$scope.data=[{"id":"1","襲高困pid":"0","name":"第一行","children":[{"id":"3","pid":"1","name":"第一行1.1"},{"id":"4","pid":"1","name":"第一行1.2"}]},{"id":"2","pid":"0","name":"第二行","children":[{"id":"5","pid":"2","name":"第二行2.1"}]}];
}]);
angular.mole("ms.leafMenu",[])
.directive('msLeafMenu',['$compile',function($compile){
return {
restrict:'EA',
transclude: true,
replace: false,
//template:"<li><念敬/li>",
scope:{
data:'=?',
id:'@?',
pid:'@?',
pvalue:'@?',
showname:'@?',
isstandard:'@?'
},
link:function(scope,element,attrs,leafController){
創建節點數據的方法:
function createTreeData(id,pid,pvalue){
var newData=[];
angular.forEach(scope.data,function(item,index){
if(item[pid]==pvalue){
var children=createTreeData(id,pid,item[id]);
if(children &&拍念 children.length>0){
item.children=children;
}
newData.push(item);
}
});
return newData;
}
if(!scope.isstandard){
scope.data=createTreeData(scope.id,scope.pid,scope.pvalue);
}
//向節點增加數據
element.append($compile('<ul class="ms_leaf_menu_group"><li ng-repeat="row in data" ng-class="{ms_parent_item:(row.children && row.children.length>0),ms_child_item:(!row.children || row.children.length<=0)}"><div ng-click="toogle($index)"><a >{{row[showname]}}</a><span class="glyphicon" ng-class="{'glyphicon-chevron-right':(row.children && row.children.length>0 && !row.isopen),'glyphicon-chevron-down':(row.children && row.children.length>0 && row.isopen)}" aria-hidden="true"></span></div><div ng-show="row.isopen"><ms-leaf-menu data="row.children" id="id" pid="pid" showname="{{showname}}" pvalue="{{row[id]}}"></ms-leaf-menu></div></li></ul>')(scope));
//此處是關鍵,調用入口處,需要找到index
scope.toogle=function(index){
scope.data[index]["isopen"]=!scope.data[index]["isopen"];
}
}
}
}]);
</script>
2、html代碼:
<body ng-app="appTest">
<div ng-controller="testCtr" style=" width:200px; margin-left:auto; margin-right:auto;">
<ms-leaf-menu data="data" id="id" pid="pid" showname="name" pvalue="0"></ms-leaf-menu>
</div>
</body>
3、效果圖
㈣ angularjs怎麼載入頁面運行函數
var mole = angular.mole('testApp', []) .controller('myC',function(){ $scope.ta = [1,2,3,4,5,6]; }) .directive('onFinishRender', function () { return { restrict: 'A', link: function (scope, element, attr) { if (scope.$last
㈤ 剛用IDEA從SVN下載下來前台angularJS的項目,要怎樣才能運行起來,求解!!
首先,程序運行環境必須要有的
項目導入到編輯軟體中
3.基本就和平時的程序差不多了
㈥ angurlajs怎麼在 html使用
將angularjs的js引入到html中
然後就可以通過各種指令、服務、事件等等來進行操作了,你剛開始可以看一下這個入門的教程 http://www.runoob.com/angularjs/angularjs-tutorial.html