【发布时间】:2026-02-16 13:55:01
【问题描述】:
我有一个奇怪的问题,我无法显示我的范围变量值。我是 Angular 新手,但我以前做过很多次。所以这里是 index.html 的主要部分。 div-ui 在 body 内部,但在这里看不到:
<input type="text" class="form-control" ng-model="searchUsers" placeholder="Search users"/>
<a ng-click="search()" ui-sref="search">Search</a>
<div ui-view>
</div>
这里是search.html:
<p>Hello world</p> // Shows normally
<p>{{test1}}</p> // Shows normally
<p>{{test2}}</p> // Nothing
<p ng-repeat="x in searchResult">{{x.username}}</p> // Nothing
<p ng-repeat="(key,value) in searchResult">{{value}}</p> // Nothing
<p ng-repeat="(key,value) in searchResult">{{value.username}}</p> // Nothing
这里是控制器:
(function(){
angular.module('TimeWaste')
.controller('NavigationCtrl', ["$scope", "$http", "$state",
function($scope,$http,$state){
$scope.searchResult = [];
// Tried with and without this
if(localStorage['User-Data']){
$scope.loggedIn = true;
}else{
$scope.loggedIn = false;
}
$scope.logUserIn = function(){
$http.post('api/user/login', $scope.login)
.success(function(response){
localStorage.setItem('User-Data', JSON.stringify(response));
$scope.loggedIn = true;
}).error(function(error){
console.log(error);
});
}
$scope.logOut = function (){
localStorage.clear();
$scope.loggedIn = false;
}
$scope.test1 = "hi";
$scope.search = function (){
$scope.test2 = "hi again";
$http.post("api/user/search", {username: $scope.searchUsers})
.success(function(response){
$scope.searchResult = response;
console.log($scope.searchResult);
// returns array of objects. There is all information that i want.
}).error(function(error){
console.log("ei");
});
}
}]);
}());
一切看起来都很正常。在搜索功能内部,它正在工作,console.log 只返回我除外的内容。我也尝试过重复 div 和表格,但我很确定这不是这里的问题。
如果有问题,这也是我的 app.js:
(function(){
angular.module('TimeWaste', ['ui.router', 'ngFileUpload'])
.config(function ($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise("/");
$stateProvider
.state("main", {
url: "/",
templateUrl: "app/main/main.html",
controller: "MainCtrl"
})
.state("search", {
url: "/search",
templateUrl: "app/main/search.html",
controller: "NavigationCtrl"
})
});
}());
还有更多的状态,它们都工作得很好。我把它缩短了一点,所以这篇文章不会那么长。
【问题讨论】:
-
$scope.searchResult = response.data? -
@Frost 你能在这里附上一个有帮助的示例回复
-
试过了。没用
-
@vickyKumar 这里是示例,这是来自 console.log 的副本:对象 v:0 _id:“57f07a9873023b15f016346c”生物:“jea”电子邮件:“ville”关注者:数组 [0]以下:数组[0]图像:“/uploads/57f07a9873023b15f016346c147537894901.jpg”密码:“ville”用户名:“moro”__proto:对象长度:1 proto:数组[0]
-
我怀疑这是原型继承的问题。尝试为您的变量使用容器对象。
$scope.data.test1然后{{data.test1}}。如果这确实解决了您的问题,我可以更详细地了解发生了什么。
标签: javascript html angularjs node.js