angular js和一些个人总结小技巧

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31214097/article/details/55669708
获取url中的网络后缀元素
  function getParam(paramName) {
        paramValue = "";
        isFound = false;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
            i = 0;
            while (i < arrSource.length && !isFound) {
                if (arrSource[i].indexOf("=") > 0) {
                    if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                        paramValue = arrSource[i].split("=")[1];
                        isFound = true;
                    }
                }
                i++;
            }
        }
        return paramValue;
    }


等待DOM元素加载完毕

    angular.element(document).ready(function () {


    });
  $scope.$on("$viewContentLoaded", function () {

    }) 
自定义指令 使用当窗体变大变小的时候出现滚动条
MainModel.directive('resize', function ($window) {
    return function (scope, element, attr) {

        var w = angular.element($window);
        scope.$watch(function () {
            return {
                'h': w.height(),
                'w': w.width()
            };
        }, function (newValue, oldValue) {
            scope.windowHeight = newValue.h;
            scope.windowWidth = newValue.w;

            scope.resizeWithOffset = function (offsetH) {

                scope.$eval(attr.notifier);

                return {
                    'height': (newValue.h - offsetH) + 'px'
                    //,'width': (newValue.w - 100) + 'px'
                };
            };

        }, true);

        w.bind('resize', function () {
            scope.$apply();
        });
    }
})
随后只需在html页面中使用resize指令即可 并且加上resizeWithOffset的宽度即可
angular js自定义指令 这个写的较好
http://www.cnblogs.com/Kavlez/p/4288885.html
封装http服务
通过$q的服务 将成功的封装到defer.resolve()中,将错误的封装到defer.reject()中 这样即可
MainModel.factory("httpService",
    function ($http, $q) {
        return {
            post: function (suburl, params) {
                var defer = $q.defer();
                $http({
                    method: 'POST',
                    params: params,
                    url: jsapi.getDomain() + suburl,
                }).success(function (data) {
                    if (data.retcode == 0) {
                        defer.resolve(data.items);
                    }
                    else
                        defer.reject(data.message);
                }).error(function (data) {
                    defer.reject(data);
                });
                return defer.promise;
            },




猜你喜欢

转载自blog.csdn.net/qq_31214097/article/details/55669708