我正在嘗試創建一個角度動態路由。我的路由是這樣的:
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', { templateUrl: 'partials/blank.html' });
$routeProvider.when('/:name', { templateUrl: 'partials/blank.html', controller: PagesController });
$routeProvider.otherwise({redirectTo: '/'});
}]);
這裏我使用$http
獲得一個控制器內部的模板文件,並將其編譯成一個div ID是這樣的:
function PagesController($scope, $http, $route, $routeParams, $compile) {
$route.current.templateUrl = 'partials/' + $routeParams.name + ".html";
$http.get($route.current.templateUrl).then(function (msg) {
$('#view-template').html($compile(msg.data)($scope));
});
}
在模板觀,我有這樣一個div:
<div id="view-template" ng-view></div>
我想上面的代碼可以編譯和HTML數據添加到div,但我收到寫着錯誤:$ is not a function
。我在這裏弄錯了什麼?
編輯:從意見和答案的幫助下
SOLUTION 後::我用這個玩弄了一點,我與這個另一種解決方案去了。我將$route.current.templateUrl
添加到$scope.theTemplateUrl
,然後在模板文件中使用ng-include
。這沒有竅門,我也不需要使用jquery $
函數來操縱DOM。
它出現的角度無法找到的jQuery。你是如何將它注入到你的應用程序中的? –
@JBNizet您的評論根本沒有幫助。對我們來說,這顯然是一個jQuery的錯誤......但顯然他不知道這一點。他也沒有要求他的控制者提供任何建議。我知道這是最好的做法,但在這種情況下並不影響他的問題。 –
@ShanRobertson:對你而言顯而易見的不是我。對我來說,就像OP認爲angular基於jQuery一樣,而且$是一個內置函數,可以像使用jQuery一樣使用它。事實並非如此,因此我的評論。只是因爲他沒有要求他的控制者提供任何建議,所以不應該阻止我幫助他,因爲他告訴他DOM操縱不應該是控制器的一部分。這個網站的重點是幫助。 –