proktovief proktovief - 13 days ago 7
AngularJS Question

Use notify.js on Angular controller

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: '/'
});
});

Answer

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!