ng-bind가 $ scope 변수에 액세스하지 않습니다. 뷰가로드되면 데이터가 렌더링되지 않습니다. 단지 비어 있지만 콘솔의 $ scope에있는 "showTopic"변수에서 액세스해야하는 모든 데이터를 볼 수 있습니다. 내가 잘못 가고있는 어떤 생각?범위 변수에 액세스 할 수없는 ng-bind
문제는 dashboard.html에서 topic.html로보기를 변경할 때 항목보기에서 발생합니다. 두보기 모두 동일한 컨트롤러를 사용하고 있습니다.
dashboard.html
<div ng-controller="topicsController">
<h2>DASHBOARD</h2>
<button class="btn btn-danger" ng-click='getCookies()'>Cookies</button>
<div class="col-sm-8 col-sm-offset-2">
<table class="table table-striped">
<thead>
<th>Category</th>
<th>Topic</th>
<th>Poster's Name</th>
<th>Posts</th>
</thead>
<tbody>
<tr ng-repeat="topic in topics track by $index">
<td> <a href="" ng-click='showTopic(topic._id, $index)'> {{topic.category}} </a></td>
<td> {{topic.topic}} </td>
<td> {{topic.user}} </td>
<td> {{topic.posts}} </td>
</tr>
</tbody>
</table>
</div>
topic.html
<div ng-controller="topicsController">
<div class="col-sm-10 col-sm-offset-1">
<h3><a href="">{{topicData.user}}</a> posted a topic:</h3>
<p>Desciption: {{ topicData.description }}</p>
<h4>Post your answer here...</h4>
</div>
topicsController.js
console.log("topicsController loaded");
app.controller('topicsController', ['$scope','topicFactory', '$location', '$cookies', function($scope, topicFactory, $location, $cookies) {
var index = function(){
topicFactory.index(function(returnedData){
$scope.topics = returnedData;
});
};
index();
$scope.add = function(){
var user = $cookies.get('user');
$scope.addTopic.user = user
topicFactory.add($scope.addTopic, function(returnedData){
index();
})
}
$scope.showTopic = function(id, $idx){
topicFactory.show(id, function(returnedData){
$scope.topicData = returnedData.data[0];
})
$location.url('/topic/' + $idx);
}
}]);
0
topicFactory.js
console.log("topicFactory loaded");
app.factory('topicFactory', ['$http', function($http) {
// The factory is nothing more than a function that returns an object
function topicFactory(){
var _this = this;
this.index = function(callback){
console.log("index topicsFactory");
$http.get('/topics').then(function(returned_data){
topics = returned_data.data;
console.log(topics);
callback(topics)
})
}
this.add = function(newTopic, callback){
console.log(newTopic);
$http.post('/topics', newTopic).then(function(returned_data){
console.log("posted");
callback(returned_data);
})
}
this.show = function(id, callback){
console.log("show factory");
$http.get('/topics/' + id).then(function(returned_data){
console.log("got show data");
callback(returned_data);
})
}
}
return new topicFactory();
}]);
그리고 콘솔에서 범위의 showTopic 변수에 볼 수있는 데이터 :
showTopic:Object
__v:0
_id:"57eca48afd30cea088ffdf2f"
category:"JavaScript"
date:"2016-09-29T05:20:10.878Z"
day:28
description:"Test 6"
month:9
topic:"Test 6"
user:"Test"
year:2016
{{$ showTopic.user}}이 (가) {{showTopic.user}}가 아니어야합니까? –
topicFactory에 대해 알아야 할 필요가 있습니다. 데이터 반환에 대기 시간이 있습니까? 약속을 사용 했습니까? –
'showTopic'은 컨트롤러에있는 함수입니다.하지만보기에 사물을 표시하려고 사용하고 있습니다. 또한 당신은'ng-controller = "topicsController"인스턴스가 2 개 있습니다. 이 질문은 매우 혼란 스럽습니다. – charlietfl