angular实现配置页面样式(保存到数据库在另一个地方使用)功能总结

需要实现的功能如图:

 一、需要对所能编辑的属性建立对象,这样会极大方便后续工作

          对于属性通常用一个angualr对象形式,所有输入框、选择框都是为其属性修改值

二、在应用的地方使用ng-style即可,其中吧属性例如backgroundImage转换为background-image需要写一个独立的函数来实现所有属性的转换

三、考虑到保存到数据库以及回显的处理

以上即完成了属性配置化,其中有亮点的地方为

1.上传图片:

上传图片使用formatData

$scope.changeChartsPicture = function (event) {
    if (event.target.files[0]) {
        var formData = new FormData();
        formData.append("filename", event.target.files[0]);
        $.ajax({
            url: '/xxxx/uploadchartsbgpicture',
            type: 'post',
            processData: false,
            contentType: false,
            data: formData
        }).then(function (data) {
            if (data.code == "1") {
                var imgPath = data.path + data.filename;
                $scope.sceneConfigObj.options.chartStyle.backgroundImage = imgPath;
                $scope.getUserChartsImgs();
            } else {
                alert('上传失败');
            }
        });
    }
}

 python代码

@api_view(http_method_names=['POST'])
@permission_classes((permissions.AllowAny,))
def uploadchartsbgpicture(request):
    f = request.FILES['filename']
    filename = time.strftime("%Y%m%d%H%M%S", time.localtime()) + f.name
    id = request.user.id
    path = './xxxx/user_' + str(id) + '/chartsbgimgs/'
    if os.path.exists(path):
        a = 1
    else:
        os.makedirs(path)
    try:
        distpath = path + filename
        if os.path.exists(distpath) and os.path.isfile(distpath):
            os.remove(distpath)
        with open(path + filename, 'wb+') as destination:
            for chunk in f.chunks():
                destination.write(chunk)
    except Exception as e:
        a = e.args
    return Response({'code': 1, "path": '/frontend/upload/user_' + str(id) + '/chartsbgimgs/', "filename": filename})

注意用request.FILES 和f.chunks来读取文件

 

两点二:用angular自定义组件

<user-imgs imgs="imgs" refresh-method="refreshStyle()" img-model="sceneConfigObj.options.globalStyle.backgroundImage"></user-imgs>
app.directive('userImgs', function () {
    return {
        restrict: 'E',
        templateUrl: '/xxxxxx/select-img.html',
        replace: true,
        scope: {
            imgs: '=', //存放图片的文件夹
            imgModel: '=',//选中事件
            refreshMethod: '&'//选中事件
        },
        link: function (scope, element, attrs, controller) {

        },
        controller: function ($scope, $http, $element, $timeout) {
            $scope.imgSelect = function (imgPath) {
                $scope.imgModel = imgPath;
                $scope.refreshMethod();
            };

            $scope.delImg = function (index) {
                $http.get(encodeURI('/api/dash/delUserImg?imgPath=' + $scope.imgs[index])).then(function (rs) {
                    if (rs.data.status == 'success') {
                        $scope.imgs.splice(index, 1);
                    }
                });
            };
        }
    };
});

directive的html

<div class="img-content">
        <div class="img-item" ng-repeat="($index, imgPath) in imgs track by $index">
            <div class="del-img" ng-click="delImg($index)">x</div>
            <img ng-click="imgSelect(imgPath)" ng-src="{{ imgPath }}">
        </div>
    </div>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是一个简单的使用 Angular 实现分页功能的例子: 首先,安装 ngx-pagination 库,这是一个方便的 Angular 分页插件。 ``` npm install ngx-pagination --save ``` 接下来,在需要分页的组件中导入 ngx-pagination: ```typescript import { NgModule } from '@angular/core'; import { NgxPaginationModule } from 'ngx-pagination'; @NgModule({ imports: [ NgxPaginationModule ] }) ``` 在组件类中定义一个数组来存储所有要展示的数据: ```typescript export class MyComponent { data = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Bob' }, { id: 4, name: 'Alice' }, { id: 5, name: 'Jim' }, { id: 6, name: 'Mary' }, { id: 7, name: 'Tom' }, { id: 8, name: 'Lucy' }, { id: 9, name: 'David' } ]; } ``` 在组件模板中,使用 ngx-pagination 指令来实现分页: ```html <table> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> <tr *ngFor="let item of data | paginate: { itemsPerPage: 3, currentPage: p }"> <td>{{item.id}}</td> <td>{{item.name}}</td> </tr> </tbody> </table> <pagination-controls (pageChange)="p = $event"></pagination-controls> ``` 在上述模板中,我们使用了 *ngFor 指令来循环渲染每一页的数据,同时使用了 paginate 指令来进行分页。其中,itemsPerPage 表示每页显示几条数据,currentPage 表示当前页码。而 pagination-controls 是 ngx-pagination 提供的组件,用来实现分页控件。 好了,这样就实现一个简单的 Angular 分页功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值