javascript - 請教AngularJS的ngRoute問題:為何這段程序無法執行?問題出在哪?
問題描述
/** index.html **/<!DOCTYPE html><html ng-app='exampleApp'><head> <title>Products</title> <script src='http://www.4tl426be.cn/wenda/angular.js'></script> <script src='http://www.4tl426be.cn/wenda/angular-route.js'></script> <script src='http://www.4tl426be.cn/wenda/products.js'></script> <link href='http://www.4tl426be.cn/wenda/bootstrap.css' rel='stylesheet' /> <link href='http://www.4tl426be.cn/wenda/bootstrap-theme.css' rel='stylesheet' /></head><body ng-controller='defaultCtrl'><p class='panel panel-primary'> <h3 class='panel-heading'>Products</h3> <p ng-view></p></p></body></html>
/** products.js **/angular.module('exampleApp', ['ngRoute']) .config(function ($routeProvider) { $routeProvider.when('/list', {templateUrl: '/tableView.html'}); $routeProvider.when('/edit', {templateUrl: '/editorView.html'}); $routeProvider.when('/create', {templateUrl: '/editorView.html'}); $routeProvider.otherwise({templateUrl: '/tableView.html'});}) .controller('defaultCtrl', function ($scope, $location) { $scope.currentProduct = null; $scope.listProducts = function () {$scope.products = [ {id: 0, name: 'Dummy1', category: 'Test', price: 1.25}, {id: 1, name: 'Dummy2', category: 'Test', price: 2.45}, {id: 2, name: 'Dummy3', category: 'Test', price: 4.25}]; }; $scope.deleteProduct = function (product) {$scope.products.splice($scope.products.indexOf(product), 1); }; $scope.createProduct = function (product) {$scope.products.push(product);$location.path('/list'); }; $scope.updateProduct = function (product) {for (var i = 0; i < $scope.products.length; i++) { if ($scope.products[i].id === product.id) {$scope.products[i] = product;break; }}$location.path('/list'); }; $scope.editOrCreateProduct = function (product) {$scope.currentProduct = product ? angular.copy(product) : {};$location.path('/edit'); }; $scope.saveEdit = function (product) {if (angular.isDefined(product.id)) { $scope.updateProduct(product);} else { $scope.createProduct(product);} }; $scope.cancelEdit = function () {$scope.currentProduct = {};$location.path('/list'); }; $scope.listProducts();});
/** tableView.html(略) **/
/** editorView.html(略) **/
程序無法執行(如圖):
問題解答
回答1:$routeProvider定義路徑時,不應該以'/'開頭
用stateProvider
相關文章:
1. angular.js - webpack build后的angularjs路由跳轉問題2. java - web項目中,用戶登陸信息存儲在session中好 還是cookie中好,取決于什么?3. 數組按鍵值封裝!4. mysql - 根據一個字段查找另一個字段重復的數據?并刪除相同的記錄,保留其中一個。5. mysql - 查詢字段做了索引為什么不起效,還有查詢一個月的時候數據都是全部出來的,如果分拆3次的話就沒問題,為什么呢。6. 這個是什么問題?7. mysql - navicat 經常打開表一直在載入中 也不能關閉 著急解決8. 單擊登錄按鈕無反應9. mysql 新增用戶 主機名設定 失敗10. mysql儲存json錯誤
