我只掌握AngularJS的基本知识。我创建了一个包含多个html页面的AngularJS应用程序。这是我的主要观点html.Im很抱歉我的问题太长了。但这是问我问题的更好方法。
Main.cshtml
<html ng-app="myapp">
<head>
<meta name="viewport" content="width=device-width" />
<title>Main</title>
<!-- Main CDN Links-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/bootstrap/3.3.1/css/bootstrap-theme.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!-- end-->
<link href="~/Content/Css/sidebar.css" rel="stylesheet" />
<style>
* {
border-radius: 0 !important;
}
</style>
</head>
<body>
<div class="nav-side-menu">
//some code
</div>
<div class="nav-top-menu nav-top">
//some code
</div>
<div class="contents" id="subContents" ui-view>
//route my sub views to here
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>
<script src="~/Scripts/smart-table.debug.js"></script>
<script src="~/Scripts/MainView.js"></script>
</body>
</html>
MainView.js
var myapp = angular.module('myapp', ["ui.router"]);
myapp.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/route1")
$stateProvider
.state('sideMenuCustomerDivition', {
templateUrl: "Main/Customer"
})
.state('sideMenuDashboardDivition', {
templateUrl: "Main/Staff"
})
})
我将另一个html视图路由到main.cshtml。这是该视图的html
Customer.cshtml
<html>
<body ng-app="customerTableApp">
<div ng-controller="safeCtrl">
<button type="button" ng-click="addRandomItem(row)" class="btn btn-sm btn-success">
<i class="glyphicon glyphicon-plus">
</i> Add random item
</button>
<table st-table="displayedCollection" st-safe-src="rowCollection" class="table table-striped">
<thead>
<tr>
<th st-sort="firstName">first name</th>
<th st-sort="lastName">last name</th>
<th st-sort="birthDate">birth date</th>
<th st-sort="balance">balance</th>
</tr>
<tr>
<th colspan="5"><input st-search="" class="form-control" placeholder="global search ..." type="text" /></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in displayedCollection">
<td>{{row.firstName}}</td>
<td>{{row.lastName}}</td>
<td>{{row.birthDate}}</td>
<td>{{row.balance}}</td>
<td>
<button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger">
<i class="glyphicon glyphicon-remove-circle">
</i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<script src="~/Scripts/CustomerView.js"></script>
</body>
</html>
CustomerView.js
var app = angular.module('customerTableApp', ['smart-table'])
app.controller('safeCtrl', ['$scope', function ($scope) {
var firstnames = ['Laurent', 'Blandine', 'Olivier', 'Max'];
var lastnames = ['Renard', 'Faivre', 'Frere', 'Eponge'];
var dates = ['1987-05-21', '1987-04-25', '1955-08-27', '1966-06-06'];
var id = 1;
function generateRandomItem(id) {
var firstname = firstnames[Math.floor(Math.random() * 3)];
var lastname = lastnames[Math.floor(Math.random() * 3)];
var birthdate = dates[Math.floor(Math.random() * 3)];
var balance = Math.floor(Math.random() * 2000);
return {
id: id,
firstName: firstname,
lastName: lastname,
birthDate: new Date(birthdate),
balance: balance
}
}
$scope.rowCollection = [];
for (id; id < 5; id++) {
$scope.rowCollection.push(generateRandomItem(id));
}
$scope.displayedCollection = [].concat($scope.rowCollection);
$scope.addRandomItem = function addRandomItem() {
$scope.rowCollection.push(generateRandomItem(id));
id++;
};
$scope.removeItem = function removeItem(row) {
var index = $scope.rowCollection.indexOf(row);
if (index !== -1) {
$scope.rowCollection.splice(index, 1);
}
}
}]);
我知道问题是ng-app路由到另一个ng-app.Anyone可以为我提供更好的解决方案。模板已经路由到视图。我的问题是我无法将值分配给第二个视图。客户视图显示在侧面主视图中。但未显示分配的值。浏览器显示该类型的错误。
错误:[ng:areq] http://errors.angularjs.org/1.3.9/ng/areq?p0=safeCtrl&p1=not%20aNaNunction%2C%20got%20undefined 在错误(本机) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:6:416 在Qb(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:19:417) 在某人(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:20:1) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:76:95 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:57:257 在s(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:7:408) 在v(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:57:124) at g(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:52:9) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js:51:118
答案 0 :(得分:1)
它可能会帮助你!!
var app = angular.module('MyApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('index', {
templateUrl: 'index.html',
controller: 'IndexCtrl'
})
.state('index.test', {
url: '/',
templateUrl: 'test.html',
controller: 'TestCtrl'
})
.state('app', {
templateUrl: 'app.html',
controller: 'AppController'
})
.state('app.test2', {
url: '/test2',
templateUrl: 'test2.html',
controller: 'Test2Controller'
})
});
另一个例子
$stateProvider
.state('index', {
url: '/',
views: {
'@' : {
templateUrl: 'layout.html',
controller: 'IndexCtrl'
},
'top@index' : { templateUrl: 'tpl.top.html',},
'left@index' : { templateUrl: 'tpl.left.html',},
'main@index' : { templateUrl: 'tpl.main.html',},
},
})
.state('index.list', {
url: '/list',
templateUrl: 'list.html',
controller: 'ListCtrl'
})
.state('index.list.detail', {
url: '/:id',
views: {
'detail@index' : {
templateUrl: 'detail.html',
controller: 'DetailCtrl'
},
},
});
答案 1 :(得分:0)
我不完全确定你的问题是什么,但是
我要注意的第一件事就是你应该从状态传递控制器。您可以.state('statename', { templateUrl: 'templateName', controller: controllerName });
此外,您可以使用状态配置对象中的url标记指定要与路径关联的URL。
.state('sideMenuCustomerDivition', {
url: '/route1',
templateUrl: 'Main/Customer',
controller: 'safeCtrl'
});
现在,如果您使用url / route1导航到路线,则应将模板插入ui-view