2

I have a controller sending parameter to a web service, when I get the response from web service I must show a notification (that's why I use notify.js), but the code is not running. I show you:

Controller:

$scope.alert = function(){
        console.log($scope.q + "/" + $scope.s.id + "/" + $scope.k.id);
        // http://localhost:8080/test/cftyu/q/q/" + q + k + s
        return $http.get('http://localhost:8080/test/cftyu/q/q/" + q + k + s)
        .success(function(data) {
            return data;
            console.log(data);
            $.notify("Hello World");
        })
        .error(function(data) {
            return data;
            console.log(data);
        });
    };
    console.log("fin controlador");

I hope anyone could help me, I know notify is called with jquery not angular I been trying to create a directive but isn't working anyway. I will appreciate any help.

Edit: I been tryint the solution Dave Cooper proposed but it's not working:

I'm trying your solution but it's not working to me. I've got this controller:

app.controller('CreateQuestionsController', ['$scope', '$log', '$http', '$window', 'sections', 'kind', 'order', 'notify',
                                       function($scope, $log, $http, 

    $window, sections, kind, order, notify) {
    $scope.alert = function(){
                return $http.get("http://localhost:8080/test/cftyu/q/q/" + q + k + s)
                .success(function(data) {
                    console.log(data);
                    notify("Hello World");
                    return data;
                })
                .error(function(data) {
                    console.log(data);
                    return data;
                });
            };
    }]);

Edit 2:

View:

<form class="form-horizontal">
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Pregunta</label>
        <div class="col-sm-10">
            <textarea class="form-control" rows="3" ng-model="question"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <label>Clase</label>
            <select class="form-control" ng-options="kind as kind.name for kind in kinds track by kind.id" ng-model="kind"></select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <label>Sección</label>
            <select class="form-control" ng-options="section as section.sectionname for section in sections track by section.id" ng-model="section"></select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-3">
            <button showNotify type="button" class="btn btn-default btn-lg" ng-click="alert()">Guardar</button>
        </div>
    </div>
</form>

I load all the js files on the 'main' view, at the bottom like this:

    <!-- Modules -->
    <script src="js/app.js"></script>

    <!-- Controllers -->
    <script src="js/controlers/HomeController.js"></script>
    <script src="js/controlers/CreateQuestionsController.js"></script>
    <script src="js/controlers/ShowQuestionsController.js"></script>

    <!-- Services -->
    <script src="js/services/questions.js"></script>

    <!-- Directives -->
    <script src="js/directives/notify.js"></script>

The controller is like I posted before

Edit 3:

There is the routes and controller-view connection:

var app = angular.module('onBuy', ['ngRoute']); 

app.config(function ($routeProvider) {
    $routeProvider
        .when('/create/questions/', {
            controller: 'CreateQuestionsController',
            templateUrl: 'views/createQuestions.html'
        })
        .otherwise({
            redirectTo: '/'
        });
});
5
  • Why don't you use libraries supporting angular like github.com/cgross/angular-notify Commented Nov 23, 2016 at 11:50
  • I tried this library but don't know why wasn't working for me Commented Nov 23, 2016 at 11:52
  • Just try according to docs provided there Commented Nov 23, 2016 at 12:06
  • I tryed and get this error: notify.js:1 Uncaught ReferenceError: directive is not defined Commented Nov 23, 2016 at 12:10
  • There's a syntax error with your quotes on line 4. Commented Nov 23, 2016 at 12:53

1 Answer 1

4

You returned from your success and error functions before displaying your notifications, so some of your code was unreachable (i.e. you were returning your data and then trying to log things and display your notification). You can see my code below for what I mean by that.

It's probably a better idea to use an Angular service like Angular Notify.

Here is what your code might look like with it - assuming you've injected the service into your controller (I've included a sample controller):

angular.module('app', ['cgNotify'])
   .controller('myController', function($scope, $http, notify) {
        $scope.alert = function(){
            return $http.get("http://localhost:8080/test/cftyu/q/q/" + q + k + s)
                .then(function(data) {
                    console.log(data);
                    notify("Hello World");
                    return data;
                }, function(data) {
                    console.log(data);
                    return data;
                });
        };
        console.log("fin controlador");
   });

Hope that helps!

edit: oh you also had a syntax error in the code you provided - I fixed that up, too!

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

9 Comments

I triyed but still don't work, I changed the code but doesn't work
I triyed the link you posted but don't know why doesn't work and I don't get any message on the js console
Can you share the rest of your code? If you could share your template with us, that would be good.
I share you the rest of the code, thank you very much
So there's a few things - 1. There's nowhere that's connecting your template to your controller, so nothing is getting hit, unless you've not included some other code, 2. I've changed my sample code to use then() instead of success() for your API call. If neither of those things help, then can you share any logs you have on your server to show that the request is actually hitting the server?
|

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.