[英]UI-Router AngularJS controller not working with state
我試圖了解UI-Router如何與Angular一起使用,但我遇到了困難。
我有一個索引:
<body>
<div ui-view></div>
<!--Location of file holding app-->
<script src="App/indexApp.js"></script>
<!--source of state2 Controller-->
<script src="App/itemsController.js"></script>
</body>
一個app.js:
var app = angular.module("ITS", ['ui.router'])
.config(['$httpProvider', '$stateProvider', '$urlRouterProvider', function ($httpProvider, $stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/home");
$stateProvider.state('home', {
url: "/home",
templateUrl: "Home.html",
controller: 'IndexCtrl'
})
$stateProvider.state('newItem', {
url: "/newItem",
templateUrl: "new.html",
controller: 'ItemCtrl'
})
控制器的文件:
app.controller("ItemCtrl", ['$scope', '$location', '$state',
function ($scope, $location, $state) {
$scope.testButton = function () { //Test FUNC
$window.alert("hey");
}
$scope.title="hello";
最后是該視圖的html頁面:
<div ng-app="ITS" ng-controller="ItemCtrl">
<body>
<button ng-click="testButton()">Test</button>
<input type="text" ng-model="title"/>
</body>
</div>
問題 :Home加載正常,我可以使用ui-sref從“ Home”導航到“ New”,並且可以看到測試按鈕。 我知道控制器正在工作並且正在訪問,但是當我單擊該按鈕時,什么都沒有彈出,並且與$ title無關的字段與“ title”關聯。
我聽說解決方案對於在狀態改變之前加載數據很重要,但是對於像這樣的問題,我似乎找不到很多關於它們的信息。 非常感激任何的幫助。
我想這可能是因為import
錯誤。 您在方法中使用$window.alert()
,但沒有將$window
作為依賴項包括在內。
嘗試像這樣向您的控制器添加$window
app.controller("ItemCtrl", ['$scope', '$location', '$state', '$window',
function ($scope, $location, $state, $window) {
$scope.testButton = function () { //Test FUNC
$window.alert("hey");
};
$scope.title="hello";
}]);
這可能有效。 同時檢查您的控制台是否有錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.