我正在学习角度,这就是为什么有一段时间我不理解代码.我有一个角度定制服务的代码.首先看代码.
angular.module('yourModule').factory('alertService', function() {
return {
showError : function() {
$.bigBox({
title: title,
content: content == null ? "An error occurred.<br /><br />If this error persists, please contact support." : content,
color: "#C46A69",
//timeout: 6000,
icon: "fa fa-warning shake animated",
//number: "1",
timeout: 3000
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
然后你可以将它注入任何控制器并使用它:
angular.module('yourModule').controller('yourController', function($scope, alertService) {
someFunction().success(function (result) {
// great!
}).error(function (error) {
// call standard error message function
alertService.showError("Update Failed"); // use default error message
});
});
Run Code Online (Sandbox Code Playgroud)
当注入内置服务然后我们使用$ sign这样的方式$ scope或$ window等但是当注入自定义的那个然后只是写没有$ sign的服务名称为什么?
如果我们需要用$ sign注入我自己的服务,那么会出现任何问题吗?对于$ sign,我需要使用任何特定的代码模式创建服务吗?
showError : function() {
}
Run Code Online (Sandbox Code Playgroud)
我们可以像这样声明上面的函数名
this.showError = function() {
};
$scope.showError = function() {
}
Run Code Online (Sandbox Code Playgroud)
如果我的理解有问题,请纠正我.
问题#1 $ sign用于角度内置服务,因此您可以区分核心服务和内置服务.建议您不要将$用于自己的服务
问题#2:没有.您正在返回一个对象,showError是对象的键,函数是值.对象键始终定义为
{
showError: function (){
}
}
Run Code Online (Sandbox Code Playgroud)
以下模式通常与控制器一起使用,而不是与服务一起使用.
this.showError = function() {
};
$scope.showError = function() {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
687 次 |
| 最近记录: |