0

I'm dividing my functions/objects into service and factory methods, and injecting them into my controller patentTab. I had a code for a tab panel which I originally placed within the controller patentTab that worked.

Now I have placed this code in a factory method and for some reason the content isn't loading. Console log shows no errors, and when I click the relative tab the correct URL is loaded, but the content doesn't change. Is there an issue with my array in the factory? If not, what is the reason?

Orginal code

app.controller('patentTab', function($scope, $http){
    $scope.tabs = [{
    title: 'Patent Information',
    url: 'patent-info.htm'
}, {
    title: 'Cost Analysis',
    url: 'cost-analysis.htm'
}, {
    title: 'Renewal History',
    url: 'renewal-history.htm'
}];

$http.get('../json/patent-info.json').then(function(response){
    $scope.patentData = response.data.patentInfo;
})

$scope.currentTab = 'patent-info.htm';

$scope.onClickTab = function (tab) {
    $scope.currentTab = tab.url; //the tabs array is passed as a parameter from the view. The function returns the url property value from the array of objects.
}

$scope.isActiveTab = function(tabUrl) {
    return tabUrl == $scope.currentTab;
}
});

New code (with issue)

 app.controller('patentCtrl', ['$scope', '$http', 'patentTabFactory', function($scope, $http, patentTabFactory) {

$http.get('http://localhost:8080/Sprint002b/restpatent/').then(function(response) {
    $scope.patents = response.data;
});

$scope.loadPatentItem = function(url) {
    $scope.patentItem = url;
}

$scope.tabs = patentTabFactory.tabs;
$scope.currentTab = patentTabFactory.currentTab;
$scope.onClickTab = patentTabFactory.onClickTab;
$scope.isActiveTab = patentTabFactory.isActiveTab;

}]);

app.factory('patentTabFactory', function() {

var factory = {};

factory.tabs = [{
    title: 'Patent Information',
    url: 'patent-info.htm'
}, {
    title: 'Cost Analysis',
    url: 'cost-analysis.htm'
}, {
    title: 'Renewal History',
    url: 'renewal-history.htm'
}];

factory.currentTab = 'patent-info.htm';

factory.onClickTab = function (tab) {
    factory.currentTab = tab.url; //the tabs array is passed as a parameter from the view. The function returns the url property value from the array of objects.
    console.log(tab.url);

}

factory.isActiveTab = function(tabUrl) {
    return tabUrl == factory.currentTab; //for styling purposes
}

return factory;

});
2
  • All looks good to me, are you defining the factory before the patentCtrl ? Commented May 17, 2017 at 8:50
  • No after? The rest of the functionality works, yet the content won't display. Don't think the error is in the HTML as it was working before hand Commented May 17, 2017 at 8:55

2 Answers 2

1

You not calling factory.onClickTab() method from your controller. It should be like :

$scope.onClickTab = function(currentTab) {
    patentTabFactory.onClickTab(currentTab);
    $scope.currentTab = patentTabFactory.currentTab;
};

and, for isActiveTab, Like :

$scope.isActiveTab = patentTabFactory.isActiveTab(currentTab);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I have spent hours looking at not understanding. I thought that when the function was called, the factory handled the execution
Glad to help you :), infact you thought right but call was not reaching to factory to handle the execution. :)
Using your same logic I had to adjust the isActiveTab to $scope.isActiveTab = function(tabUrl) { return tabUrl == patentTabFactory.currentTab; }
yes you have to, but like the way i already mentioned in answer $scope.isActiveTab = patentTabFactory.isActiveTab(currentTab);
0

Here is a plunker where I am using a factory. The only changes I have done are: 1. Place the factory file before the app script file. 2. Use a separate declaration for factories and then inject it in the app.

var factories = angular.module('plunker.factory', []);
factories.factory('patentTabFactory', function() {
// Factory bits
};

I have injected the factories in the app.

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

Here is a working plunker for that. PlunkR

2 Comments

I could be missing something form the demo but I don't think it provides the outcome I'm looking for. Can I ask why two modules have been declared? Is this best practice?
I indeed is the best practise. Helps segregate the factories from everything else. It also helps in making tests more easy to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.