gpt4 book ai didi

javascript - 学习到 angular : ng-view not loading, 为什么?

转载 作者:行者123 更新时间:2023-11-30 16:50:31 25 4
gpt4 key购买 nike

这个应用程序的页面只是加载空白。我搜索了几个小时的问题,但仍然找不到问题。 Angular 是 1.3.5,Angular-route 是 v1.2.28。

这是 index.html:

<html ng-app="myApp">
<head>
<script src="js/angulark.min.js"></script>
<script src="js/angular-routek.js"></script>
<script>
var myApp = angular.module('myApp', ['ngRoute']);

myApp.config(function ($routeProvider) {
$routeProvider
.when('/',
{
controller: 'MainController',
templateUrl: 'views/nameView.html';
})
.when('/cityView',
{
controller: 'MainController',
templateUrl: 'views/cityView.html';
})
.otherwise({ redirectTo: '/' });
});

myApp.controller('MainController', ['$scope', function($scope) {
$scope.customers = [
{ name: 'Andre Queiroz', city: 'Rio de Janeiro' },
{ name: 'Fulano', city: 'Sao Paulo'},
{ name: 'Beltrano', city: 'Curitiba' }
];

$scope.addCustomer() = function () {
$scope.customers.push(
{ name: $scope.newCustomer.name, city: $scope.newCustomer.city }
);
};
}]);
</script>
<title>Meu Aplicativo</title>
</head>
<body>
<div>
<!-- Placeholder for views -->
<div ng-view> </div>
</div>
</body>
</html>

这是nameView.html

<div class="container">	
<div ng-controller="MainController">
<div>
<h2>View 1</h2>
Name: <input type="text" ng-model="filter.name"/>
</div>
<br />
<ul>
<li ng-repeat="cust in customers | filter:filter.name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city}}</li>
</ul>
</div

<div>
<p>Customer name:</p>
<input type="text" ng-model="newCustomer.name" />
<p>Customer city:</p>
<input type="text" ng-model="newCustomer.city" />
<button ng-click="addCustomer()">Add customer </button>
<a href="#/view2">View 2</a>
</div>
</div>
</div>

cityView.html 是相同的,但没有 addCustomer 内容。我是分成模块文件等等,但我把它放在一个文件中,看看它是否有效。

最佳答案

index.html javascript 中存在错误。在 MainController 中更改您的代码

$scope.addCustomer() = function () {
$scope.customers.push(
{ name: $scope.newCustomer.name, city: $scope.newCustomer.city }
);
};

$scope.addCustomer= function () {
$scope.customers.push(
{ name: $scope.newCustomer.name, city: $scope.newCustomer.city }
);
};

它应该是 $scope.addCustomer= 而不是 $scope.addCustomer()=

关于javascript - 学习到 angular : ng-view not loading, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com