Last active
December 20, 2015 02:59
-
-
Save cades/6059950 to your computer and use it in GitHub Desktop.
示範如何在config()中呼叫provider提供的函式。 這個例子寫了一個pathRootProvider, 注意其中兩個被return的object: 第一個是 provider()回傳的物件,注入config()的就是它。第二個是$get()回傳的物件,注入controller的就是它。 關鍵在於「把函式提出$get, 拉到與$get同一個層級」,放在這裡的函式才能在config()內被呼叫。 順帶一提,factory()是provider()的一個方便使用的包裝,傳給factory的function會被放進$get中,也因此失去在config()中被呼叫的可能性。 Robin Fan提供一個影片,解說的頗清楚:http://www.egghead.io/video/HvTZb…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp', ['ui.state']). | |
provider('pathRoot', function() { | |
return { | |
partialView: function(filename) { | |
return '../js/angular/partial/' + filename; | |
}, | |
$get: function(pathId) { | |
return {}; | |
} | |
}; | |
}). | |
config(function(pathRootProvider, $stateProvider, $urlRouterProvider) { | |
$stateProvider | |
.state('list', { | |
url: '/list', | |
templateUrl: pathRootProvider.partialView('list.html') | |
}) | |
.state('new', { | |
url: '/new', | |
templateUrl: pathRootProvider.partialView('new.html') | |
}); | |
}). | |
controller('MyCtrl', function($scope) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment