python , angular js 学习记录【2】

1.不同scope之间的通信

(1)无父子关系的scope通信:

在需要操作的scope里面定义一个事件,名称为delete_host,参数为data

$rootScope.$on('delete_host', function(event,data) {
            angular.forEach($scope.hosts, function (item, i) {
                if (item.id == data) {
                    $scope.hosts[i].deleted = true;
                }
                if($scope.selected_host && $scope.selected_host.id == data){
                    $scope.selected_host = null;
                }

            })
        });

在需要触发该事件的scope里面触发

$rootScope.$emit('delete_host',$scope.selected_host.id);

注意为rootscope,emit

(2)父子关系下scope通信

<div  ng-controller="FatherCtrl">
    <div  ng-controller="ChildCtrl">
    
    </div>
</div>

父scope里面写事件,传递参数为data

$scope.$on('change-breadcrumb', function(event,data) {
            $scope.breadcrumb = Util.breadcrumb("h"+$routeParams.id,data);
        });

子scope里面触发事件,注意为emit

$scope.$emit('change-breadcrumb',newValue)

子scope里面写事件,传递参数为data

$scope.$on('showdetail-storage', function(event,data) {
            $scope.selected_storage = data ;
        });

父scope里面触发事件,注意为broadcast

$scope.$broadcast('showdetail-storage',storage);

备注:父类向子类触发事件 用boradcast

子类向父类触发事件 用emit

2.定时任务

linux里面的cron可以实现定时任务。

crontab -e 编辑执行周期 以及执行方法
00 00 * * * python (路径)/license_day_count.py  每天00:00分执行该py文件
service crond stop 关闭服务 service crond start开启服务
/var/spool/mail/root 可查看部分信息

打开base64加密的文件,定时更新,并加密存入文件

daycountfile = open('/opt/filename"')
try:
    daycountstr = daycountfile.read().strip()
    daycount = int(base64.decodestring(daycountstr))
    daycount = daycount - 1
    file_writer = open('/opt/filename'', 'w')
    file_writer.write(base64.encodestring(str(daycount)))
    file_writer.close()
finally:
    daycountfile.close()

3.代码冗余处理

从List<Entity>里面获取id的列表

storage_ids = [s.id for s in storages]
storage_names = [s.name for s in storages]

从LIst<JsonObj>里面获取某个属性的列表

alivenames = [s.get('storage') for s in result]

比较storage_names和alivenames ,如果后者缺少某个值,则进行某些操作

for storage in storages:
                    if storage.name not in alivenames:
                        result.append({"storage": storage.name, "name": ""})
原文地址:https://www.cnblogs.com/cuiyf/p/4686231.html