Angular框架
打開編譯環境(webstorm),在工程文件夾里新建一個html文件
在ctrl.html文件夾引入所需要的包和工具,如bootstrp和angular等
在angular.js文件下方定義一個angular模塊(module),代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp =augular.module('myapp',[])//定義一個模塊
</script>
</body>
</html>
定義一個控制器,并用scope搭建視圖與網頁通信的橋梁,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp =augular.module('myapp',[])//定義一個模塊
myapp.controller=("myctrl",['scrope',function () {
}])
</script>
</body>
</html>
在function隨便定義一個變量,一般為scope,網頁頭部<html>引入模塊名:ng-app="myapp",在網頁主體內容<body>里面引入控制器ng-controller="myctrl"這樣,網頁的內容才能被MVC控制器所接收和識別,代碼如下:
<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body ng-app="ng-controller">
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp =augular.module('myapp',[])//定義一個模塊
myapp.controller=("myctrl",['scope',function (scope) {
}])
</script>
</body>
</html>
在控制器里面定義所需要的數據與內容,代碼如下:
<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body ng-app="ng-controller">
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp =augular.module('myapp',[])//定義一個模塊
myapp.controller=("myctrl",['scope',function (scope) {
scope.articelelist=[
{'title':'新聞資訊','content':'新聞內容顯示'},
{'title':'小說連載','content':'擇天記'},
{'title':'生活百家','content':'社會相關'},
{'title':'軍事演練','content':'軍事圖片'},
{'title':'科技','content':'手機、IT'}
]
}])
</script>
</body>
</html>
在<body>里面定義一個響應式容器,一個panel組,遍歷(ng-repeat)數組數據,在panel-headding顯示文章標題的內容,代碼如下:
<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body ng-controller="myctrl">
<div class="container">
<div class="panel-group">
<div class="panel panel-default" ng-repeat="artic in articeleList">
<div class="panel-heading">
<p class="panel-title">{{artic.title}}</p>
</div>
</div>
</div>
</div>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp = angular.module('myapp',[]);//定義一個模塊
//定義一個控制器
myapp.controller('myctrl',['scope',function (scope) {
scope.articeleList=[
{'title':'新聞資訊','content':'新聞內容顯示'},
{'title':'小說連載','content':'擇天記'},
{'title':'生活百家','content':'社會相關'},
{'title':'軍事演練','content':'軍事圖片'},
{'title':'科技','content':'手機、IT'}
];
}])
</script>
</body>
</html>
運行程序。
在panel-body中顯示網頁內容,代碼如下:
<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body ng-controller="myctrl">
<div class="container">
<div class="panel-group">
<div class="panel panel-default" ng-repeat="artic in articeleList">
<div class="panel-heading">
<p class="panel-title">{{artic.title}}</p>
</div>
<div class="panel-body">
<p class="panel-content">{{artic.content}}</p>
</div>
</div>
</div>
< /div>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myapp = angular.module('myapp',[]);//定義一個模塊
//定義一個控制器
myapp.controller('myctrl',['scope',function (scope) {
scope.articeleList=[
{'title':'新聞資訊','content':'新聞內容顯示'},
{'title':'小說連載','content':'擇天記'},
{'title':'生活百家','content':'社會相關'},
{'title':'軍事演練','content':'軍事圖片'},
{'title':'科技','content':'手機、IT'}
];
}])
</script>
</body>
</html>
運行程序,得到最終結果。
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!