从多个Controller&获取数据使用AngularJs将项目发布到服务器中?

时间:2016-08-29 07:15:33

标签: angularjs asp.net-mvc

我有一个以上的控制器&通过该控制器,我们希望得到数据和最后,想将该数据发布到dbserver。,

下面我添加了服务&控制器如何使用“我得到的最后一个推送值只能我如何修改服务”plz建议我是angularJs的新手。我很困惑,谢谢你

 //Module
 var app = angular.module('application', []);


 //Service
app.service('InstanceService', function () {
var instancedetails = [];

var addinstance = function (newObj) {
    instancedetails.push(newObj);
};

var getInstances = function () {
    return instancedetails;
};

return {
    addinstance: addinstance,
    getInstances: getInstances
};

});

//controller1
app.controller('page2ctrl', function ($scope, $http, InstanceService) {

$scope.Page2data = null;
$scope.message = "";

$scope.instancedata = {
    InstanceID: '',
    CompanyName: '',
    AccessName: '',
    FullName: '',
    EmailAddress: '',
    MobileNumber: ''
};



$scope.submit = function () {
    if ($scope.instancedata.CompanyName != "" && $scope.instancedata.AccessName != "") {

        InstanceService.addinstance($scope.instancedata);

        window.location = 'Page3'
  }

    else {
        alert('Please Enter All the Values !!');
    }

}

});


//Controller2 
app.controller("bannerCtrl",function ($scope, $http, InstanceService) {
$scope.bnnrdata = {
    Banner: ''
}
$scope.savebnnr = function () {
    InstanceService.addinstance($scope.bnnrdata);

    window.location = 'Ammineties';


}
});

//Controller3 
app.controller("AmminitesCtrl", function ($scope, $http, InstanceService) {

$scope.ammdata = {
    Currency: '',
    Language: '',
    Timezone: ''
};
$scope.saveAmm = function () {
    InstanceService.addinstance($scope.ammdata);

    $scope.Inst = InstanceService.getInstances();

    $http({
        method: 'POST',
        url: '/api/Instance/postitem',
        data: $scope.Inst
    });
    window.location = 'Page5';


   }
});

2 个答案:

答案 0 :(得分:0)

我希望以下内容能完成您的工作。我怎么没有测试它,因为我没有你的HTML和评论如果你遇到任何错误。我修改的行的前缀是注释 //行修改如下

var app = angular.module('application', []);


 //Service
app.service('InstanceService', function () {
var instancedetails = [];

var addinstance = function (newObj) {
    instancedetails.push(newObj);
};

var getInstances = function () {
    return instancedetails;
};

return {
    instancedetails:instancedetails,
    addinstance: addinstance,
    getInstances: getInstances
};

});

//controller1
app.controller('page2ctrl', function ($scope, $http, InstanceService) {

$scope.Page2data = null;
$scope.message = "";

$scope.instancedata = {
    InstanceID: '',
    CompanyName: '',
    AccessName: '',
    FullName: '',
    EmailAddress: '',
    MobileNumber: ''
};



$scope.submit = function () {
    /*Directly assign it to that object*/
    //Line Modified as below
  InstanceService.addinstance.push($scope.instancedata);

    if ($scope.instancedata.CompanyName != "" && $scope.instancedata.AccessName != "") {
//If your checking some conditions add the line here 
   //     InstanceService.addinstance($scope.instancedata);
//Line Modified as below
        window.location = '/Page3'
  }

    else {
        alert('Please Enter All the Values !!');
    }

}

});


//Controller2 
app.controller("bannerCtrl",function ($scope, $http, InstanceService) {
$scope.bnnrdata = {
    Banner: ''
}
$scope.savebnnr = function () {
//Line Modified as below
    InstanceService.instancedetails.push($scope.bnnrdata);
//Line Modified as below
    window.location = '/Ammineties';


}
});

//Controller3 
app.controller("AmminitesCtrl", function ($scope, $http, InstanceService) {
 //added
 $scope.Inst=[];
$scope.ammdata = {
    Currency: '',
    Language: '',
    Timezone: ''
};
$scope.saveAmm = function () {
    //Line Modified as below
    InstanceService.instancedetails.push($scope.ammdata);

    //Line Modified as below
    $scope.Inst = InstanceService.instancedetails;

    $http({
        method: 'POST',
        url: '/api/Instance/postitem',
        data: $scope.Inst
    });
    //Line Modified as below
    window.location = '/Page5';
       }
});

答案 1 :(得分:0)

使用过" window.location =' / page5'"通过这整个服务&重定向到下一页。将重新创建所有对象,因此我得到的值为null&最后推送的值将出现在该数组中。

所以我用而不是window.location ==> " $ location.path(' /第5页');"通过这解决了我的错误 感谢所有人的建议。