简体   繁体   English

Angular ng-bind 不访问范围变量

[英]Angular ng-bind not accessing scope variables

ng-bind is not accessing the $scope variables. ng-bind 没有访问 $scope 变量。 When the view loads the data is not rendered - it's just blank, but I can see all of the data I need to access under the "showTopic" variable in the $scope in the console.当视图加载时,数据未呈现 - 它只是空白,但我可以在控制台 $scope 的“showTopic”变量下看到我需要访问的所有数据。 Any ideas where I'm going wrong?任何想法我哪里出错了?

The problem occurs on the topic view when I follow a change views from dashboard.html to topic.html.当我按照从dashboard.html 到topic.html 的更改视图时,主题视图上会出现问题。 Both views are using the same controller.两个视图都使用相同的控制器。

dashboard.html仪表盘.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主题.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主题控制器.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);
  }

}]);

topicFactory.js主题工厂.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();
}]);

And the data that I can view in the showTopic variable in scope in the console:以及我可以在控制台范围内的 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

The problem is occurring because of your view change.问题是由于您的视图更改而发生的。 When you change views the scope changes as well.当您更改视图时,范围也会发生变化。 The scope exists in your dashboard view with correct scope variables, but is actually empty in the topic view.该范围存在于您的dashboard视图中,具有正确的范围变量,但在topic视图中实际上是空的。 I know it looks deceiving because you are logging the object.我知道它看起来是骗人的,因为您正在记录对象。 So if you are changing views and want to use scope variables from another view/controller, you will want to save the object in a service and then in a controller return that object, assign the object fields to your scope variables and display it that way, which is what you are doing, but you are using the same controller for two different views.因此,如果您正在更改视图并希望使用来自另一个视图/控制器的范围变量,您需要将对象保存在服务中,然后在控制器中返回该对象,将对象字段分配给您的范围变量并以这种方式显示,这就是您正在做的事情,但是您对两个不同的视图使用相同的控制器。 Once the second loads it becomes empty and nothing is showing.一旦第二个加载它变空,什么都没有显示。

What I suggest doing is creating two different controllers.我建议做的是创建两个不同的控制器。 The first to capture the topics you need, pass to service and store them, then call them in the new controller and they will display.第一个捕获您需要的主题,传递给服务并存储它们,然后在新控制器中调用它们并显示它们。 Two different views, two different controllers, one service to access the info.两种不同的视图,两种不同的控制器,一种访问信息的服务。

I highly suggest looking at this post and understand how to pass scopes between routes in Angular and how views affect the scope.我强烈建议查看这篇文章并了解如何在 Angular 中的路由之间传递范围以及视图如何影响范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM