AngularJS服务factory,service,provider,value,constant

AngularJS服务

AngularJS提供了一些内置服务,在任何地方使用它们的方式都是统一的。同时,为复杂应用创建我们自己的服务也是非常有用的。

在AngularJS中创建自己的服务是非常容易的:只需要注册这个服务即可。服务被注册后,AngularJS编译器就可以引用它,并且在运行时把它当作依赖加载进来。服务名称的注册表使得在测试中伪造和剔除相互隔离的应用依赖变得非常容易。

注册一个服务

.factory(“testFac”,function(){
return{
getA:function(a){
return a;
},
getB:function(b){
c(b)
}
}
})

其中这个服务包含了2个方法,只需要在控制器中引用testFac就可以直接使用这2个方法,就可以了;这只是一次服务演示。当然,这2个方法是非常枯燥无味的;但是,假设你需要对用户信息进行验证,或者你需要读取用户的一些信息,那么这个服务就不枯燥无味了。

linux计划任务crontab例子

/root/send_msg.sh #要自动执行的脚本程序路径

chmod +x /root/send_msg.sh #对脚本文件添加执行权限,否则不能执行

vim /etc/crontab #编辑配置文件,在最后一行添加内容

30 1 * * * root /root/send_msg.sh #表示每天凌晨1点30执行备份

:wq! #保存退出

service crond restart / service crond reload #重启/重载配置文件

crontab文件的格式:

minute hour day month weekday username command
minute:分,值为0-59
hour:小时,值为1-23
day:天,值为1-31
month:月,值为1-12
weekday:星期,值为0-6(0代表星期天,1代表星期一,以此类推)
username:要执行程序的用户,一般设置为root
command:要执行的程序路径(设置为绝对路径)例如:/root/send_msg.sh
附:crontab规则详细实例

angularJS学习笔记

QQ截图20160929161813

<script>
    function c(str) {
        console.log(str)
    }

    var app = angular.module("Myapp", ['ngRoute'])
            .config(['$locationProvider',function($locationProvider){
                $locationProvider.html5Mode(false);
                $locationProvider.hashPrefix("!")
            }])
            .config(function ($routeProvider) {

                $routeProvider
                        .when('/', {
                            templateUrl: 'views/main.html',
                            controller: 'MainCtrl'
                        })
                        .when('/about', {
                            templateUrl: 'views/about.html',
                            controller: 'AboutCtrl',

                        })
                        .when('/action', {
                            templateUrl: 'views/action.html',
                            controller: 'ActionCtrl'
                        })
                        .when('/contact:q', {
                            templateUrl: 'views/contact.html',
                            controller: 'ContactCtrl'
                        }).when("/register", {
                            templateUrl: 'views/register.html',
                            controller: 'registerCtrl'
                        })
                        .otherwise({
                            redirectTo: '/'
                        })
            })
            .run(['$rootScope','$location',function ($rootScope,$location) {
                $rootScope.title = "网站的标题";
                $rootScope.keywords = "网站的关键词";
                $rootScope.description = "网站的描述";

                //路由开始
                $rootScope.$on('$routeChangeStart',function(evt, next, current){

                });

                //路由成功
                $rootScope.$on('$routeChangeSuccess', function(evt, next, previous) {

                });

                //路由错误
                $rootScope.$on('$routeChangeError', function(current, previous, rejection) {
                    c('$routeChangeError')
                    c(current)
                    c(previous)
                    c(rejection)
                });

            }])
            .factory('myFactory', function(){
                var service = {1:"a",2:"b",3:"c"};
                return service;
            })
            .directive('myDirective', function(){
                return {
                    template: '<button>Click me</button>'
                }
            })
            .directive('ensureUnique', function ($http) {
                return {
                    require: 'ngModel',
                    link: function (scope, ele, attrs, cfg) {
                        scope.$watch(attrs.ngModel, function () {
                            $http({
                                method: 'POST',
                                url: '/1.php',
                                params: {filed: attrs.name, value: attrs.ngModel, value2: ele[0].value}

                            }).success(function (data, status, headers, fg) {
                                c(data);

                            }).error(function (data, status, headers, fg) {
                                alert('网络繁忙!');
                            })
                        });
                    }
                };
            })

            .controller('MainCtrl', function ($scope, $routeParams) {
                $scope.context = "Main页面标题";
                $scope.add = function (num) {
                    ++num;
                    alert("num:" + num);
                };
                $scope.name = {name: "a001", pwd: "a123456"};

            })
            .controller('AboutCtrl', function ($scope, $routeParams, $filter) {
                $scope.context = "关于我们";
                $scope.other = "谢谢支持!";

//

                //$scope.name = $filter('lowercase');
                $scope.unClock = function () {
                    $scope.isDisabled = true;
                    // alert('ok');
                }
                $scope.myChange = function (d) {
                    c(d);
                }
                $scope.submitForm = function (d) {
                    c(d);
                }

                $scope.people = [
                    {name: "小A", city: "重庆"},
                    {name: "小b", city: "成都"},
                    {name: "小c", city: "武汉"},
                    {name: "小d", city: "广州"},
                    {name: "小e", city: "上海"}
                ];

                $scope.fields = [
                    {isRequired: true, name: "user_name", placeholder: "请输入用户名", model1: "", msg: "请输入用户名"},
                    {isRequired: true, name: "user_pwd", placeholder: "请输入用户密码", model1: "", msg: "请输入用户密码"},
                    {isRequired: true, name: "email", placeholder: "请输入用户邮箱", model1: "", msg: "请输入用户邮箱"},
                ]
                $scope.cx = "testcx"


            }).controller('ActionCtrl', function ($scope, $routeParams) {
                $scope.context = "Action测试页面";


            }).controller('ContactCtrl', function ($scope, $routeParams) {
                $scope.context = "联系人";


            }).controller("nowDae", function ($scope) {

                $scope.now = new Date();
                $scope.do = function () {

                    $.ajax({
                        url: '/1.php',
                        type: 'post',
                        dataType: 'json',
                        async: false,
                        data: {id: 10, user_name: 'a001', user_pwd: '123456'},
                        success: function (data) {
                            c("data>>>>>");
                            c(data);
                        }
                    })
                }

            }).controller("registerCtrl", function ($scope, $routeParams) {
                $scope.context = "用户注册";
                $scope.showAgreement = function () {
                    alert("用户协议...");
                };

                $scope.doreg = function () {

                    alert('sdaf');
                    return false;
                }


            })
            .filter('number2', function () {
                return function (input) {

                    return "过滤过的:" + input;


                };
            }).filter('email2', function () {
                return function (input) {
                    if (input) {
                        return "filter:" + input;
                    }

                };
            })



</script>

代码包下载:百度网盘

http://pan.baidu.com/s/1kUH1H0F

AngularJS多重视图和路由教程

多重视图和路由

能够从页面的一个视图跳转到另外一个视图,对单页面应用来讲是至关重要的。当应用变得越来越复杂时,我们需要一个合理的方式来管理用户在使用过程中看到的界面。也许我们已经通过在主HTML中添加行内的模板代码,实现了对视图的管理,但这些代码也会变得越来越复杂和难以管理,同时其他开发者也很难加入到开发工作中来。另外,由于应用的状态信息会包含在URL中,我们也无法将代码直接复制并发送给朋友。除了用 ng-include 指令在视图中引用多个模板外,更好的做法是将视图分解成布局和模板视图,并且根据用户当前访问的URL来展示对应的视图。我们会将这些模板分解到视图中,并在布局模板内进行组装。AngularJS允许我们在 $route服务的提供者 $routeProvider 中通过声明路由来实现这个功能。通过 $routeProvider ,可以发挥出浏览器历史导航的优势,并让用户基于浏览器当前的URL地址创建书签或分享页面。

安装

从1.2版本开始,AngularJS将 ngRoutes 从核心代码中剥离出来成为独立的模块。我们需要安装并引用它,才能够在AngularJS应用中正常地使用路由功能。

可以从code.angularjs.org下载它,然后保存到一个可以在HTML页面中进行引用的位置,例如js/vendor/angular-route.js。也可以用Bower来安装,这样会将它存放到Bower的目录中。

CentOS下安装build-essential,gcc,gfortran/g77编译器

安装完成gcc后发现 gcc -v 和g++ -v都能显示,唯独gfortran -v时显示 gfortran命令没有找到,说明gfortran没有安装。
命令安装如下:
(1)命令安装gcc
yum install gcc
(2)安装g++
yum install gcc-c++
结果显示gcc-c++已被成功安装,所以这样运行安装gfortran应该也不会出现问题
(3)安装gfortran
yum install gcc-gfortran
然后一路yes。。
运行gfortran -v 显示安装版本。。成功。。

补充g77的安装。。

依旧是在/media/CentOS_6.2_Final/Packages目录下:

[root@TTWORKTEAM Packages]# yum search g77
结果显示:
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
=============================== N/S Matched: g77 ===============================
compat-gcc-34-g77.x86_64 : Fortran 77 support for compatibility compiler

Name and summary matches only, use “search all” for everything.

可以看到有一个软件包compat-gcc-34-g77.x86_64为g77编译器 ,命令安装如下:

[root@TTWORKTEAM Packages]# yum install compat-gcc-34-g77-3.4.6-19.el6.x86_64.rpm
过程显示:(一直选择Y,默认安装。。。)
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Examining compat-gcc-34-g77-3.4.6-19.el6.x86_64.rpm: compat-gcc-34-g77-3.4.6-19.el6.x86_64
Marking compat-gcc-34-g77-3.4.6-19.el6.x86_64.rpm to be installed
Resolving Dependencies
–> Running transaction check
—> Package compat-gcc-34-g77.x86_64 0:3.4.6-19.el6 will be installed
–> Processing Dependency: compat-gcc-34 = 3.4.6-19.el6 for package: compat-gcc-34-g77-3.4.6-19.el6.x86_64
–> Processing Dependency: compat-libf2c-34 = 3.4.6-19.el6 for package: compat-gcc-34-g77-3.4.6-19.el6.x86_64
–> Processing Dependency: libg2c.so.0()(64bit) for package: compat-gcc-34-g77-3.4.6-19.el6.x86_64
–> Running transaction check
—> Package compat-gcc-34.x86_64 0:3.4.6-19.el6 will be installed
—> Package compat-libf2c-34.x86_64 0:3.4.6-19.el6 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
compat-gcc-34-g77 x86_64 3.4.6-19.el6 /compat-gcc-34-g77-3.4.6-19.el6.x86_64
5.9 M
Installing for dependencies:
compat-gcc-34 x86_64 3.4.6-19.el6 base 3.7 M
compat-libf2c-34 x86_64 3.4.6-19.el6 base 51 k

Transaction Summary
================================================================================
Install 3 Package(s)

Total size: 9.6 M
Total download size: 3.8 M
Installed size: 19 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): compat-gcc-34-3.4.6-19.el6.x86_64.rpm | 3.7 MB 00:00
(2/2): compat-libf2c-34-3.4.6-19.el6.x86_64.rpm | 51 kB 00:00
——————————————————————————–
Total 7.2 MB/s | 3.8 MB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : compat-gcc-34-3.4.6-19.el6.x86_64 1/3
Installing : compat-libf2c-34-3.4.6-19.el6.x86_64 2/3
Installing : compat-gcc-34-g77-3.4.6-19.el6.x86_64 3/3

Installed:
compat-gcc-34-g77.x86_64 0:3.4.6-19.el6

Dependency Installed:
compat-gcc-34.x86_64 0:3.4.6-19.el6 compat-libf2c-34.x86_64 0:3.4.6-19.el6

Complete!

安装完成,用命令g77 -v检测安装是否成功和查看安装路径。

15455565758118
 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |