• angularJs之service


          

    自定义服务:

    方法一:controller中返回值,service中return

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular. min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myCtrl">

    <p>000000000</p>

    <h1>{{hex}}</h1>

    </div>

    <p>自定义服务:</p>

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

    app.service('hexafy', function() {
    this.myFunc = function (x) {
    return 56;
    }
    });
    app.controller('myCtrl', function($scope, hexafy) {
    $scope.hex = hexafy .myFunc(255);

    });
    </script>

    </body>
    </html>

     方法一:controller中无返回值,service中无return,直接将值存入$scope

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
    <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular. min.js"></script> 
    </head> 
    <body> 
    <div ng-app="myApp" ng-controller="myCtrl">

    <p>000000000</p>

    <h1>{{hex}}</h1>

    </div>

    <p>自定义服务:</p>

    <script> 
    var app = angular.module('myApp', []);app.service('hexafy', function() { 

    this.myFunc = function (scope,x) { 
    scope.hex =56; 

    }); 
    app.controller('myCtrl', function($scope, hexafy) { 
     hexafy .myFunc($scope,255); 

    }); 
    </script>

    </body> 
    </html>

    不需要去new service

  • 相关阅读:
    解决验证码问题
    python中的偏函数partial
    Python 数据表orm设计
    参悟yield 和yield from (加精)
    numpy基础
    JS中的面向对象
    pandas使用教程
    一次tornado建站项目分享
    django 一对一, 一对多,多对多的领悟
    三维向量类
  • 原文地址:https://www.cnblogs.com/songyunxinQQ529616136/p/6231859.html
Copyright © 2020-2023  润新知