一个简易版的Angular js 三层 示例

var myApp = angular.module('produceline', []);
myApp.factory('ajax', ["$http", "$q", "$log", function ($http, $q, $log) {
    return {
        //post请求,第一个参数是URL,第二个参数是向服务器发送的参数(JSON对象),  
        post: function (url, data, funsuccess) {
            var deferred = $q.defer();
            var tempPromise;
            if (data != null && data != undefined && data != "") {
                tempPromise = $http.post(url, data);
            } else {
                tempPromise = $http.post(url);
            }
            tempPromise.success(function (data, header, config, status) {
                deferred.resolve(data);
            }).error(function (msg, code) {
                $log.error(msg, "post url:" + url + " 出错,代码:" + code);
                deferred.reject(msg);
            });
            return deferred.promise.then(funsuccess);
        },
        //get请求,第一个参数是URL,第二个参数是向服务器发送的参数(JSON对象), 此方法有问题  
        get: function (url, data, funsuccess) {
            var deferred = $q.defer();
            var tempPromise;
            //判断用户是否传递了参数,如果有参数需要传递参数  
            if (data != null && data != undefined && data != "") {
                tempPromise = $http.get(url, data); //params  {params: {id:5}}
            } else {
                tempPromise = $http.get(url);
            }
            tempPromise.success(function (data, header, config, status) {
                deferred.resolve(data);
            }).error(function (msg, code) {
                deferred.reject(msg);
                $log.error(msg, "get Url:" + url + " 出错,代码:" + code);
            });
            return deferred.promise.then(funsuccess);
        }
    };
}]);
myApp.controller('producelineController', ['$scope', "ajax", function ($scope, ajax) {
    //扫描枪中止符 13 为回车
    $scope.endWidthScan = 13;

    $scope.model = {};

    $scope.partSegments = []; //生产线分段

    $scope.init = function () {
        $("#scantxt").focus();
        $.extend($scope.model, modeljson);
        $scope.partSegments = partSegments || [];
        //for (var i = 0; i < $scope.partSegments.length; i++) {
        //    $scope.partcodesmodel[$scope.partcodes[i]] = '';
        //}
    };


    $scope.init();


    $scope.SaveSacnedProduct = function (product) {
        //一次保存一个扫描项 
        var data = {};
        data[product] = $scope.ScanedHash[product];
        ajax.post($scope.model.UrlLineSave, { 'id': $scope.model.ID, 'json': angular.toJson(data) }, function (response) {
            console.log('get ' + $scope.model.UrlLineSave + ' return t:' + response.t);
        })
    };



    $scope.newSegment = function () {
        var seg = { SegmentName: '', SegmentCode: '', Remark: '' };
        $scope.partSegments.push(seg);
    };

    $scope.delSegment = function (idx,array) {
        array.splice(idx, 1)
    };


    $scope.ProductScanComplete = function () {
        $scope.CopyToHash($scope.ScaningProduct);
        $scope.ScaningProduct = "";
        $scope.complete.push($scope.ScaningProduct)
        $scope.PlaySuccess();

    }

    $scope.IsProductComplete = function (product) {
        if (product == '' || product == null) {
            return false;
        }
        for (var i = 0 ; i < $scope.partcodes.length ; i++) {
            var t = $scope.partcodesmodel[$scope.partcodes[i]];
            if (t && t != null && t != '') {
                continue;
            }
            return false;
        }
        return true;
    };
    $scope.AllEmpty = function (product) {
        for (var i = 0 ; i < $scope.partcodes.length ; i++) {
            var t = $scope.partcodesmodel[$scope.partcodes[i]];
            if (t == '') {
                continue;
            }
            return false;
        }
        return true;

    };



    
    $scope.showJson = function () {

        alert(angular.toJson(
            {
                //'complete': $scope.complete,
                //'partcodes': $scope.complete,
                //'partcodesmodel': $scope.partcodesmodel,
                ScanedHash: $scope.ScanedHash,
                ID: $scope.ID
                //ScaningProduct: $scope.ScaningProduct,
                //ScanedPartHash: $scope.ScanedPartHash,
                //samecodecount: $scope.samecodecount
            })
            );
    };

}]);

  

posted on 2018-10-22 15:43  LovlyPuppy  阅读(230)  评论(0编辑  收藏  举报

导航