2016-11-23 95 views
2

我有一个控制器向Web服务发送参数,当我得到Web服务的响应时,我必须显示一个通知(这就是为什么我使用notify.js),但代码没有运行。我告诉你:在角度控制器上使用notify.js

控制器:

$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"); 

我希望有人可以帮助我,我知道通知被称为与jQuery没有棱角我一直在努力创造一个指令,但无论如何都不会工作。我会感谢任何帮助。

编辑: 我去过tryint溶液戴夫库珀提议但它不灵:

我试图您水溶液却不工作我。我有此控制器:

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; 
       }); 
      }; 
    }]); 

编辑2:

查看:

<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> 

我加载在 '主' 视图中的所有js文件,在这样的底部:

<!-- 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> 

控制器就像是我

前发布

编辑3:

有路由和控制器视图连接:

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

app.config(function ($routeProvider) { 
    $routeProvider 
     .when('/create/questions/', { 
      controller: 'CreateQuestionsController', 
      templateUrl: 'views/createQuestions.html' 
     }) 
     .otherwise({ 
      redirectTo: '/' 
     }); 
}); 
+0

为什么你不使用类似https://github.com/cgross/angular-notify支持'angular'的库 –

+0

我试过这个库,但不知道为什么不能为我工作 – proktovief

+0

只是试着根据提供那里的文档 –

回答

3

你从你的成功和错误显示功能,您的通知之前回来,所以你的一些代码是无法访问(即您正在返回数据,然后尝试记录事件并显示您的通知)。您可以在下面看到我的代码,以了解我的意思。

使用类似Angular Notify这样的Angular服务可能更好。

这里是你的代码是什么样子吧 - 假设你已经注入的服务为您的控制器(我已经包含了样本控制器):

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"); 
    }); 

希望帮助!

编辑:哦,你在你提供的代码中也有语法错误 - 我也修正了这个错误!

+0

我triyed,但仍然不工作,我改变了代码,但不起作用 – proktovief

+0

我对你发布的链接进行了三化处理,但不知道为什么不起作用,并且我没有在js控制台上收到任何消息 – proktovief

+0

你可以分享其他代码吗?如果你可以与我们分享你的模板,那会很好。 –