• 【ng】【js】【笔记】angularJs常用


    【ng】【js】【笔记】angularJs常用


    1、select尽量用ng-options,不用ng-repeat

    <select class="form-control" ng-model="pstm.ExtractionSampleTypeId" ng-options="typeItem.ExtractionSampleTypeId as typeItem.SampleType for typeItem in ProductExtractionSampleDTO">
        <option value="{{typeItem.ExtractionSampleTypeId}}">{{typeItem.SampleType}}</option>
    </select>

    2、弹窗和弹窗回调
    ----------父级页面----------
    <!--父级页面ng控件声明-->
    <div class="container" ng-controller="控件声明" ng-init="Init()" ng-cloak>
    //js调用layer弹窗 文档https://www.layui.com/doc/modules/layer.html#layer.confirm
    layerIndex = layer.open({
        type: 2,
        title: title, //显示标题
        area: ['90%', '100%'], //宽高
        content: url,
        cancel: function () {
            //取消后
            scope.Search(1);
        }
    });
    //声明弹窗回调方法
    scope.SelectedItemBack(selectedItem){
         layer.confirm('确定要(操作)吗?', { icon: 3, title: '提示' }, function (index) {
            Ajax.post(apiurl, {
            }, function (result) {
                if (index) {
                    layer.close(index);
                }
                layer.msg("操作成功")
                scope.Search(1)
            });
        });
    }
    ----------弹窗页面----------
    <!--弹窗上选择行对象-->
    <td class="" ng-if="isSelected">
        <button type="button" class="btn btn-success btn-xs" ng-click="SelectedItem(item)"> 选择</button>
    </td>
    //ng调用弹窗的父级页面的ng方法
    scope.SelectedItem = function (item) {

        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);//关闭当前窗口

        //回调,选择器选到父级的ng域scope,调用回调方法
        parent.angular.element("div[ng-controller='控件声明']").scope().SelectedItemBack(item);
    }

    3、ng模块配置
    var NgModelName = angular.module('NgModelName', []);
    NgModelName.config(function ($httpProvider) {
        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    })

    4、ng过滤filter
    //时间格式设置,调用:{{item.CreateTime | jsonDate:4}}
    NgModelName.filter("jsonDate", function ($filter) {
        //{{data | jsonDate:4}}
        //formatType:默认 "yyyy-MM-dd HH:mm:ss" , 1 "yyyy-MM-dd "
        return function (input, formatType, text) {
            var format = "yyyy-MM-dd HH:mm:ss";
            if (!input) {
                return text ? text : "";
            }
            if (formatType && !isNaN(formatType)) {
                switch (formatType) {
                    case 1:
                        format = "yyyy-MM-dd";
                        break;
                    case 2:
                        format = 'yyyy-MM-dd HH:mm'
                        break;
                    case 3:
                        format = 'yyyy-MM'
                        break;
                    case 4:
                        format = 'yyyy-MM-dd HH:mm:ss'
                        break;
                    default:
                        format = "yyyy-MM-dd";
                        break;
                }
            }
            //先得到时间戳
            var timestamp = Number(input.replace(//Date((d+))//, "$1"));

            //转成指定格式
            return $filter("date")(timestamp, format);
        }
    });

    //html标签
    NgModelName.filter("showAsHtml", function ($sce) {
        return function (input) {
            return $sce.trustAsHtml(input);
        }
    });

    5、常用方法
    重新渲染scope.$apply();或scope.$apply(function(){//执行部分});
    异步重新渲染scope.$applyAsync();
    事件禁止冒泡<input ng-click="SetIsBind($event)" /> , scope.SetIsBind=function(e){if (e) {e.stopPropagation();}}

  • 相关阅读:
    POJ3984 输出路径
    ACWING 844.走迷宫
    N皇后问题 DFS解法(给出两种)
    Codeforces Round #632 (Div. 2)
    HDU--1198 Farm Irrigation (并查集做法+DFS做法)
    HDU--1325 并查集判树(有向无环图)
    python接口自动化(二十) 发xml格式post请求
    python接口自动化(十九) 签名(signature)鉴权(authentication)之加密(HEX,MD5,HMAC-SHA256)
    python接口自动化(十八) 下载excel文件(Content-Type:octets/stream)
    python接口自动化(十七) requests获取响应时间(elapsed)与超时(timeout)
  • 原文地址:https://www.cnblogs.com/lanofsky/p/14096572.html
Copyright © 2020-2023  润新知