将几个函数添加到一个控制器角度

时间:2016-09-23 18:15:05

标签: angularjs ionic-framework

我试图将另一个功能添加到我的控制器,但它一直打破控制器。

这是我的代码:

.controller('ClimbController', [
	'$scope', '$stateParams', 'Climbs', function(
	$scope, $stateParams, Climbs) {

		var climb_id = $stateParams.climbId;
		var areaId = $stateParams.areaId;

		if (!isNaN(climb_id)) {
			climb_id = parseInt(climb_id);
		}

		if (!isNaN(areaId)) {
			areaId = parseInt(areaId);
		}

		$scope.selected_ = {};
		$scope.items = [];
		$scope.details = true;
		// looping though all data and get particular product
		$scope.selectClimb = function(areas){
			areas.forEach(function(data) {
			    if(data._id == climb_id){
			    	$scope.selected_ = data;
			    }
			});
		}
		// get all posts // try some function to get a single produt from server
		$scope.getPosts = function(){
			Climbs.getPosts()
			.success(function (data) {
				// data = feed.json file

				var climbs = [];
				data.areas.map(function(area) {
					if (area._id === areaId) {
						climbs = area.climbs;
					}
				});
				$scope.selectClimb(climbs);
			})
			.error(function (error) {
				$scope.items = [];
			});
		}
		$scope.getPosts();
}
])

我想把它添加到它:

.controller('MyCtrl', function($scope, $ionicModal) {
  $ionicModal.fromTemplateUrl('test-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function() {
    $scope.modal.show();
  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  //Cleanup the modal when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
  // Execute action on hide modal
  $scope.$on('modal.hidden', function() {
    // Execute action
  });
  // Execute action on remove modal
  $scope.$on('modal.removed', function() {
    // Execute action
  });
});

当我尝试将其添加到代码时,它会破坏它。我需要将它添加为另一个函数或将其添加到代码中所需的任何内容。

非常感谢

1 个答案:

答案 0 :(得分:1)

假设您要将'MyCtrl个函数合并到ClimbController然后

.controller('ClimbController', ['$scope', '$stateParams', 'Climbs','$ionicModal', function($scope, $stateParams, Climbs,$ionicModal) {

        var climb_id = $stateParams.climbId;
        var areaId = $stateParams.areaId;

        if (!isNaN(climb_id)) {
            climb_id = parseInt(climb_id);
        }

        if (!isNaN(areaId)) {
            areaId = parseInt(areaId);
        }

        $scope.selected_ = {};
        $scope.items = [];
        $scope.details = true;
        // looping though all data and get particular product
        $scope.selectClimb = function(areas){
            areas.forEach(function(data) {
                if(data._id == climb_id){
                    $scope.selected_ = data;
                }
            });
        }
        // get all posts // try some function to get a single produt from server
        $scope.getPosts = function(){
            Climbs.getPosts()
            .success(function (data) {
                // data = feed.json file

                var climbs = [];
                data.areas.map(function(area) {
                    if (area._id === areaId) {
                        climbs = area.climbs;
                    }
                });
                $scope.selectClimb(climbs);
            })
            .error(function (error) {
                $scope.items = [];
            });
        }
        $scope.getPosts();


        $ionicModal.fromTemplateUrl('test-modal.html', {
            scope: $scope,
            animation: 'slide-in-up'
          }).then(function(modal) {
            $scope.modal = modal;
          });

          $scope.openModal = function() {
            $scope.modal.show();
          };
          $scope.closeModal = function() {
            $scope.modal.hide();
          };
          //Cleanup the modal when we're done with it!
          $scope.$on('$destroy', function() {
            $scope.modal.remove();
          });
          // Execute action on hide modal
          $scope.$on('modal.hidden', function() {
            // Execute action
          });
          // Execute action on remove modal
          $scope.$on('modal.removed', function() {
            // Execute action
          });

}])