user1155141 user1155141 - 2 months ago 10
AngularJS Question

adding several functions to one controller angular

Im trying to add another function to my controller but it keeps breaking the controller.

here is my code:



.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();
}
])





And I ned to add this to it:



.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
});
});





When I try to add this to the code it breaks it. I nee to either add it as another function or whatever is needed to add it to the code.

Thanks so much

Answer

Assuming that you want to merge 'MyCtrl functions into ClimbController then

.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
          });

}])
Comments