当然,我应该提一下,我是这个人的新手,很抱歉,如果这是微不足道的。
所以我几乎有2条路线(视图)。 localhost:3000
接收并加载对象列表,localhost:3000/:slug
显示用户希望查看更多信息的产品信息。
首次上市是好的。您访问localhost:3000
并看到项目列表。
listPhoneController.js:
angular.module('cmscApp').controller('listPhoneController', ['$scope', '$http', '$location', 'searchBoxFactory',
function($scope, $http, $location, searchBoxFactory) {
$scope.listInfo = searchBoxFactory;
$scope.phoneList = [];
$http.get('/api/getallphones').then(function(res) {
$scope.phoneList = res.data;
}, function() {
$scope.errorMsg = 'Error in reaching data';
});
}]);
list.html:
<!-- ... --->
<div class="result" ng-repeat="phone in phoneList | hasImageFilter:listInfo.imageRequired
| nameFilter:listInfo.phoneName
| priceFilter:listInfo.price.from:listInfo.price.to">
<!-- filters don't seam to be the problem (removing them still causes the issue) -->
<a ng-href="/phone.slug">More info...</a>
<!-- ... -->
</div>
现在,如果我点击a
标记,我会被重定向到该手机的信息视图(即localhost:3000/samsung-galaxy-s4
)并正确加载信息。我还有一个后退按钮,只有一个简单的<a ng-href='/'>Back</a>
但是,当我返回时,即使网址更改回localhost:3000
,列表也不会显示。我没有错误,没有任何错误,但div
不存在(检查时,也没有)。
这是因为$http
是异步的,所以它会在获取信息之前尝试加载页面吗?如果是这种情况,为什么它不像往常一样只绑定数据?
这是我的配置:
angular.module('cmscApp').config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: '/pages/list.html',
controller: 'listPhoneController',
controllerAs: 'lpc'
})
.when('/:phoneSlug', {
templateUrl: '/pages/detail.html',
controller: 'detailPhoneController',
controllerAs: 'dpc'
})
.otherwise({
templateUrl: '/error/404.html'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
任何形式的帮助都非常受欢迎!我考虑将数据从$http
存储到工厂,然后每次运行控制器时加载该数据,同时更新它。那会是一个可行的解决方案,还是有更好的东西?
答案 0 :(得分:0)
由于某种原因,结果范围没有绑定,所以我不得不编辑$http.get().then()
方法:
$http.get(...).then(function(res) {
$scope.data = res;
$scope.$apply();
}, function() { ... });
遇到类似问题的人