我使用angular構建了一個小應用程序,添加了routeProvider以更改我的ContentView。在我不同的htmls之間切換的作品,我可以在我的HTML使用汽車和功能。
var app = angular.module("mainApp", ['ngRoute']);
angular.module("mainApp").config(
[ '$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider.when("/", {
templateUrl : './page1.html',
controller : 'con1'
}).when("/stuff", {
templateUrl : './page2.html',
controller : 'con2'
}).when("/404", {
templateUrl : "./errorPage.html"
})
// else 404
.otherwise({
redirectTo : "/404"
});
} ]);
我增加了功能:
this.$onInit = function() {console.log("test"); };
做的東西時,我的控制器處於初始化模式。 (注意:我在其他項目中添加了這個模式,但是在這個新項目中它不會加載這個。$ onInit)
我將Angular 1.6.1用作Webjar和。
任何想法,爲什麼它不會工作?
相同的代碼只能在一個項目中工作,而不能在其他項目中工作?在哪個控制器中添加了'$ onInit'生命週期鉤子,你可以發佈控制器的一些代碼嗎?你能澄清一下你的問題嗎?它看起來像[this](https://jsfiddle.net/aygqcgeb/2/)? –
如果加載asinged html,我的其他(第一個項目)實現相同的模式調用控制器的init函數。而這個項目不會稱之爲它。有點奇怪。 – Offset
是不是像'this。$ onInit();'手動調用某處?你有沒有檢查我的答案和[jsfiddle](https://jsfiddle.net/aygqcgeb/9/)例子? –