在 Angular 控制器上使用 notify.js
Use notify.js on Angular controller
我有一个控制器向 Web 服务发送参数,当我从 Web 服务收到响应时,我必须显示一个通知(这就是我使用 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 调用的,而不是 angular 我一直在尝试创建一个指令,但无论如何都不起作用。我将不胜感激任何帮助。
编辑:
我一直在尝试 Dave Cooper 提出的解决方案,但它不起作用:
我正在尝试您的解决方案,但它对我不起作用。我有这个控制器:
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>
我在 'main' 视图中加载了所有的 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: '/'
});
});
您在显示通知之前从成功和错误函数返回,因此您的某些代码无法访问(即您正在返回数据,然后尝试记录内容并显示通知)。您可以在下面查看我的代码,了解我的意思。
最好使用 Angular 服务,例如 Angular Notify。
你的代码可能看起来像这样 - 假设你已经将服务注入你的控制器(我已经包含了一个示例控制器):
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");
});
希望对您有所帮助!
编辑:哦,您提供的代码中也有语法错误 - 我也修复了它!
我有一个控制器向 Web 服务发送参数,当我从 Web 服务收到响应时,我必须显示一个通知(这就是我使用 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 调用的,而不是 angular 我一直在尝试创建一个指令,但无论如何都不起作用。我将不胜感激任何帮助。
编辑: 我一直在尝试 Dave Cooper 提出的解决方案,但它不起作用:
我正在尝试您的解决方案,但它对我不起作用。我有这个控制器:
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>
我在 'main' 视图中加载了所有的 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: '/'
});
});
您在显示通知之前从成功和错误函数返回,因此您的某些代码无法访问(即您正在返回数据,然后尝试记录内容并显示通知)。您可以在下面查看我的代码,了解我的意思。
最好使用 Angular 服务,例如 Angular Notify。
你的代码可能看起来像这样 - 假设你已经将服务注入你的控制器(我已经包含了一个示例控制器):
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");
});
希望对您有所帮助!
编辑:哦,您提供的代码中也有语法错误 - 我也修复了它!