2

I'm new to AngularJs and encountered a problem while trying to avoid writing the same code in different controllers.

I've created a factory that should hold all functions, while the controllers are able to use these functions, and moved a function to that factory from the controller. I created a function which should post data from a form, but when I click on it to execute, literally nothing happens.

I've searched quite a while on google and stackoverflow and couldn't find any issue that fits my problem.

Is there something I missed or did wrong?

Factory:

 (function(){
  angular.module("myApp").factory('appServicesProvider',function( $http ) {

    var restURL = "http://localhost:8080/Project/rest/api/";

  function postFunction(data){

        $http.post(restURL, JSON.stringify(data)).then(
                function(response){
                }           
        );
  }

  return{postFunction:postFunction}

}); 
})();

Controller:

(function() {

angular.module("myApp")
.controller("AdminController",function($scope, $http, appServicesProvider) {

$scope.restURL = "http://localhost:8080/Project/rest/api/";

)}; // There's more code but it's irrelevant to the function I'm talking 
       about

HTML:

  <div id="postFunctionDiv" class="form-group row">
  <div class="col-xs-4">
  <label>PostFunction</label>

<!---
Some form inputs
---!>

<button  class="btn  btn-success" ng-
 click="appServicesProvider.postFunction(data)"  >Execute</button>
 </div>

2 Answers 2

3

ng-click should call a scope function within the controller, rather than attempting to call a method within the factory directly. That controller function will be what calls the factory method. Example:

Controller:

(function() {

angular.module("myApp")
.controller("AdminController",function($scope, $http, appServicesProvider) {

$scope.restURL = "http://localhost:8080/Project/rest/api/";

$scope.postFn = function(data) {
    appServicesProvider.postFunction(data);
};

)}; // There's more code but it's irrelevant to the function I'm talking 
       about

HTML:

  <div id="postFunctionDiv" class="form-group row">
  <div class="col-xs-4">
  <label>PostFunction</label>

<!---
Some form inputs
---!>

<button  class="btn  btn-success" ng-
 click="postFn(data)"  >Execute</button>
 </div>

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I see what you did there :) Thank you so much!!
1

The problem with appServicesProvider's postFunction isn't getting called because, you haven't exposed appServicesProvider service on $scope. In short whatever exposed in $scope will be accessible on html.

angular.module("myApp")
.controller("AdminController",function($scope, $http, appServicesProvider) {

   $scope.appServicesProvider = appServicesProvider

)};

Above will just solve your problem, that wouldn't be good way to go for as you exposed everything from service on HTML unnecessarily. Rather exposed only desired service method on $scope by creating your own method postFunction.

angular.module("myApp")
.controller("AdminController",
 function($scope, $http, appServicesProvider) {
   $scope.postFunction = function (data) {
       appServicesProvider.postFunction(data)
   }
 }
);

HTML

ng-click="postFunction(data)" 

3 Comments

@DorGolan my bad. I had typo in my code. I corrected. Can you try please?
Now it's the same code as Kyle suggested above, which works :) Thanks!
@DorGolan yeah.. I had that before as well, but missed to change the function name :) I didn't copied from his code :p

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.