0

I'm very new to angularjs and SPA.

I was trying to use the navbar structure of bootstrap adding a login section as a dropdown in a index spa. I want to click on the sign in button and call the function to authenticate and hide the dropdown of login when the authentication is ok.

At the beginning I didn't include the controller in the HTML, but then I included the attribute ng-controller and I don't know why I'm getting this error

Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20MyService

So if I remove the ng-controller="MyController" I don't get the error but I cannot call the method from the controller and I tried this before but without any success.

angular.element(document.getElementById('frmMyController')).scope().authenticate();

But as I said if add the ngcontroller my app will generate the injection error.

I pasted my code in jsfiddle but it doesn't generate any error.

So, how can I call the method authenticate with the button sign in correctly and then if the response is ok, how to hide the dropdown button signin and show the profile authenticated?

1 Answer 1

0

It seems to be an injection error. Try to inject the $scope like this :

app.controller('MyController', ['$scope', 'MyService',function($scope, MyService){
    $scope.authenticate = function(username, password){
        MyService.authenticate(username, password);
    }
}])

You have to do it for all your components (config, service, etc).

I don't know which build system you are using, but if you are using gulp, you can add gulp-annotate to your build stream. It will add the injection's array automatically.

Edit 1

Remove the $scope from your service.

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

5 Comments

Thanks for your answer. I still get the same error. I don't know why in this particular case it doesn't work. In other pages I declared the controller as I show in the example and I don't get this error.
@MaximusDecimus remove the $scope from your service.
Yeah. We did the same at the same time! Thank you.
But now if I get any response as ok. Do you know how to hide the login button with angular?
@MaximusDecimus If my answer did satisfy your needs, please check it. For the last question, create another post.

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.