g00*_*0fy 8 angularjs angularjs-service
我想广告一个新的(对话)type到角,所以就像我用我可以使用它module.directive,module.filter,module.controller注册指令,过滤器和控制器.
我想以dialog这种方式注册我的类型实例:
module.dialog('prompt',function(dependencies){
return {
templateUrl:'prompt.html',
controller:function($scope){},
something:'value'
}
});
Run Code Online (Sandbox Code Playgroud)
我还希望能够在控制器中使用已注册的对话框(依赖注入)
module.controller('ListCtrl',function($scope,prompt){
$scope.deleteItem = function(item){
prompt('Do you want to delete this item?').then(function(result){
if(result) item.$delete();
});
}
});
Run Code Online (Sandbox Code Playgroud)
这归结为以下问题:
如何扩展角度模块以module.dialog注册我的dialog类型?
如何将注册的dialogs注射剂注入controllers等?
顺便说一句,
angular-ui和angular-strap.dialog作服务,而是单独使用type(此解决方案已经实现angular-ui).这是一个非常有趣的问题.我将在答案前加上一个观点:我认为你不应该延伸angular.module提供一种dialog方法.这些方法是内置Angular提供程序的快捷方式,Angular团队不时添加一些方法.既然您可以在不添加dialog方法的情况下访问您正在寻找的功能,我就不会.也就是说,下面的代码确实向您展示了它的基本版本如何工作(它不会修改Angular模块原型,只修改模块的单个实例).
<div ng-app="myApp">
<div ng-controller='MainController'>
<div>
<button ng-click='askName()'>Ask Name</button>
<button ng-click='askNameAgain()'>Ask Name Again</button>
<button ng-click='askAge()'>Ask Age</button>
<button ng-click='askFood()'>Ask Food</button>
</div>
<div>{{lastResponse}}</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
var app = angular.module('myApp', []);
// Provide some basic injectables for testing
app.constant('nameString', 'NAME');
app.constant('ageString', 'AGE');
app.constant('foodString', 'FAVORITE FOOD');
// Create the dialog provider
app.provider('dialog', function($provide, $injector) {
var dialogs = {};
this.register = function(name, configFn) {
// Create a new service
$provide.factory(name, function($window, $q) {
dialogs[name] = function() {
// Get data based on DI injected version of configFn
var data = $injector.invoke(configFn);
// faking async here since prompt is really synchronous
var deferred = $q.defer();
var response = $window.prompt(data.text);
deferred.resolve(response);
return deferred.promise;
};
return dialogs[name];
});
};
// Injecting the service itself gives you a function that
// allows you to access a dialog by name, much like $filter
this.$get = function() {
return function(name) {
return dialogs[name];
};
};
});
// Providing dialog injectables via app.config
app.config(function(dialogProvider) {
dialogProvider.register('askFood', function(foodString) {
return { text: 'What is your ' + foodString + '?' }
});
});
// Alternatively, shortcut to accessing the dialogProvider via app.dialog
app.dialog = function(name, configFn) {
app.config(function(dialogProvider) {
dialogProvider.register(name, configFn);
});
};
app.dialog('askName', function(nameString) {
return { text: 'What is your ' + nameString + '?' }
});
app.dialog('askAge', function(ageString) {
return { text: 'What is your ' + ageString + '?' }
});
app.controller('MainController',
function($scope, askName, askAge, askFood, dialog) {
var setLastResponse = function(result) {
$scope.lastResponse = result;
};
$scope.askName = function() {
askName().then(setLastResponse);
};
$scope.askNameAgain = function() {
// get the dialog through the dialog service
// much like how $filter works
var theDialog = dialog('askName');
theDialog().then(setLastResponse);
};
$scope.askAge = function() {
askAge().then(setLastResponse);
};
$scope.askFood = function() {
askFood().then(setLastResponse);
};
});
Run Code Online (Sandbox Code Playgroud)
这是一个工作示例:http://jsfiddle.net/BinaryMuse/zj4Jq/
通过利用函数$injector.invoke内部dialogProvider.register,您可以提供使用返回controller数据中的键的功能configFn.由于directive已经很多这样的工作,你可以通过检查AngularJS源获得很多.
| 归档时间: |
|
| 查看次数: |
5727 次 |
| 最近记录: |