AngularJS 的使用进阶(一)

2017-08-13  本文已影响138人  屋檐下的燕雀

之前有写一篇文章介绍了 AngularJS 的简单使用,只是写了一些AngularJS的简单用法,经过一段时间的学习,这里总结了一些 AngularJS 的进行一些进阶使用。(这里的实例均使用angularJS 1.4.6版本)
功能介绍和使用
这里对要使用的 AngularJS 功能进行一些介绍:

<!--ng-app 声明 AngularJS 名字,ng-controller 声明了 控制器-->
<div ng-app="myApp" ng-controller="myCtrl">
<!--ng-model 将 input 的输入值 同 $scope.name 互相绑定-->
    名字: <input ng-model="name">
</div>
<!--此处为 AngularJS 的主要程序代码-->
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
});
</script>

示例代码:

<p>姓名为 {{ lastName | lowercase }}</p>
<p>货币为 {{ currency | currency }}</p>
<p>输入过滤:</p>
<p><input type="text" ng-model="test"></p>
<ul>
  <li ng-repeat="x in names | filter:test | orderBy:'country'">
    {{ (x.name | uppercase) + ', ' + x.country }}
  </li>
</ul>
</div>
<script>
    var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.lastName = "John Doe";
    $scope.currency = 11111;
    $scope.names = [
        { 'name':'KAI','country':'Denmark'},
        { 'name':'JANI','country':'Norway'},
        { 'name':'HEGE','country':'Sweden'}
    ];
    
});
</script>

运行结果:

AngularJS-filter.PNG

输入过滤条件结果:


AngularJS-filter1.PNG
<div ng-app="myApp" ng-controller="myCtrl">

<p>原姓名:{{msg}}</p>
<!-- 使用自定义的过滤器 : reverse  -->
<p>姓名: {{ msg | reverse }}</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.msg = "Runoob";
});
//创建自定义的过滤器 - reverse
app.filter('reverse', function() { //可以注入依赖
    return function(text) {
          //将字符串 text 内容倒置
        return text.split("").reverse().join("");
    }
});
</script>

运行结果:


AngularJS-自定义filter.PNG
<div ng-app="myApp" ng-controller="myCtrl">
<p> 当前页面的url:</p>
<h3>{{myUrl}}</h3>
</div>
<p>该实例使用了内置的 $location 服务获取当前页面的 URL。</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});
</script>

运行结果:


AngularJS-service-location.PNG

自定义服务:

<div ng-app="myApp" ng-controller="myCtrl">
<p>255 的16进制是:</p>
<h1>{{hex}}</h1>
</div>
<p>自定义服务,用于转换16进制数:</p>
<script>
var app = angular.module('myApp', []);
//自定义服务-hexafy,数据转换成16 进制
app.service('hexafy', function() {
    this.myFunc = function (x) {
        return x.toString(16);
    }
});
app.controller('myCtrl', function($scope, hexafy) {
//自定义服务-hexafy
  $scope.hex = hexafy.myFunc(255);
});
</script>
// 简单的 GET 请求,可以改为 POST
$http({
    method: 'GET',//post 请求改为‘POST’
    url: '/someUrl'
}).success(function successCallback(response) {
        // 请求成功执行代码
    }).error(function() {
            // 请求失败执行代码
        });
//注意:此处使用的版本是AngularJS 1.4.6,在AngularJS 1.5中$http 的 success 和 error 方法已废弃。使用 then 方法替代。使用格式如下:
/*
// 简单的 GET 请求,可以改为 POST
$http({
    method: 'GET',
    url: '/someUrl'
}).then(function successCallback(response) {
        // 请求成功执行代码
    }, function errorCallback(response) {
        // 请求失败执行代码
});
*/

除了上面的方法,AngularJS 还提供了很多简单写法:
POST 与 GET 简写方法格式:
$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
更多简写方式可参见:https://docs.angularjs.org/api/ng/service/$http

除此之外,使用 $http 还可以读取 JSON 文件
以下是存在本地的JSON 文件:

[
    {"id":1,"work_name":"楚乔传","upload_state":"是"},
    {"id":2,"work_name":"楚乔传","upload_state":"是"}
]

代码示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl"> 
<ul>
  <li ng-repeat="item in datas">
    {{item.work_name + ',' + item.upload_state}}
  </li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
//json 文件路径
var url1="../conf/datalist.json";
app.controller('myCtrl', function($scope, $http) {
    $http.get(url1).success(function (response) {
        $scope.datas = response;
    });
});
</script>
</html>

运行结果:

AngularJS-http.PNG

总结

此处暂时就写这几个功能,AngularJS 还有很多实用的功能,待到以后在继续写。

上一篇 下一篇

猜你喜欢

热点阅读