[英]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.