zoukankan      html  css  js  c++  java
  • AngularJS 服务

    AngularJS 服务

    AngularJS 中可以创建自己的服务,或使用内建服务。

    在 AngularJS 中,服务是一个函数或对象,可在AngularJS 应用中使用。

    AngularJS 内建了30 多个服务。

    $location 服务,返回当前页面的 URL 地址。

    var app = angular.module('myApp', []);

    app.controller('customersCtrl', function($scope, $location) {

        $scope.myUrl = $location.absUrl();

    });

    $http 向服务器发送请求,应用响应服务器传送过来的数据。

    AngularJS 会一直监控应用,处理事件变化, AngularJS 使用 $location 服务比使用 window.location 对象更好。

    var app = angular.module('myApp', []);

    app.controller('myCtrl', function($scope, $http) {

        $http.get("welcome.htm").then(function (response) {

            $scope.myWelcome = response.data;

        });

    });

    <div ng-app="myApp" ng-controller="customersCtrl"> 

        <ul>

            <li ng-repeat="x in names">

                {{ x.Name + ', ' + x.Country }}

            </li>

        </ul>

    </div>

    <script>

        var app = angular.module('myApp', []);

        app.controller('customersCtrl', function($scope, $http) {

            $http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")

            .success(function(response) {$scope.names = response.records;});

        });

    </script>

    AngularJS $timeout 服务对应了 JS window.setTimeout 函数。

    var app = angular.module('myApp', []);

    app.controller('myCtrl', function($scope, $timeout) {

        $scope.myHeader = "Hello World!";

        $timeout(function () {

            $scope.myHeader = "How are you today?";

        }, 2000);

    });

    AngularJS $interval 服务对应了 JS window.setInterval 函数。

    var app = angular.module('myApp', []);

    app.controller('myCtrl', function($scope, $interval) {

        $scope.theTime = new Date().toLocaleTimeString();

        $interval(function () {

            $scope.theTime = new Date().toLocaleTimeString();

        }, 1000);

    });

    创建自定义服务

    app.service('hexafy', function() {

        this.myFunc = function (x) {

            return x.toString(16);

        }

    });

    app.controller('myCtrl', function($scope, hexafy) {

        $scope.hex = hexafy.myFunc(255);

    });

  • 相关阅读:
    ASP.NET Core Web API 帮助页
    SQL SERVER 被锁住的表,以及解锁。
    关键字查找相关存储过程,函数和视图
    MsSql 生成数据文档
    WebApiTestClient 接口测试页
    sql日期
    为什么同样的Sql语句在SqlServer RDS 查询得到的和自己本机SqlServer 查询的不一样呢?
    VS 无法在web服务器上启动调试。您没有调试web服务器进程的权限
    ROS学习之创建工作空间
    QT学习之forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget"
  • 原文地址:https://www.cnblogs.com/DreamFather/p/11326892.html
Copyright © 2011-2022 走看看