Having trouble setting $scope after redirecting to a partial template via routing

Looking to create a website that loads all necessary templates upon the initial visit. Currently, I only have one partial template but plan to add more in the future. Despite having just this one template, I'm struggling with binding the data from my API into the HTML.

HTML

<!DOCTYPE html>
    <html lang="en" ng-app="tophueApp">
        <body>
            <div ng-view></div>

            <script type="text/ng-template" id="two_column.html">
                <div id="two_column_container">
                    <div id="left_pane">
                    <div class="material_background">
                        <div ng-repeat="post in posts" class="post_block">
                            <a href="/post/{{post.pk}}/{{post.url_safe_title}}" class="title_text" onclick="return show_inline(this,'{{post.source_site}}')">
                                <span ng-if="post.is_NSFW" class="is_NSFW">
                                    NSFW
                                </span>

                                {{post.title}}
                            </a>
                <span class="post_info">
                    </span>
                </span>
                </div>
                </div>
                    </div>
                </div>
            </script>

            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.js"></script>
            <script src="base.js"></script>
            <link rel="stylesheet" href="base.css" />
        </body>
    </html>

base.js

var tophueApp = angular.module('tophueApp', ['ngRoute']);

tophueApp.config(['$routeProvider','$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.
      when('/topic/:topicName/', {
        templateUrl: 'two_column.html',
        controller: 'TopicController'
      }).
      when('/', {
        templateUrl: 'two_column.html',
        controller: 'HomeController'
      }).
      otherwise({
        redirectTo: '/'
      });

     $locationProvider.html5Mode({
                     enabled: true,
                     requireBase: false
              });
  }]);

tophueApp.controller('TopicController', ['$scope', '$http', '$routeParams',
    function($scope, $http, $routeParams) {
        $http.get('/api/topic/'+$routeParams.topicName+'/').success(function(data) {
        $scope.posts = data;
    });
}]);

tophueApp.controller('HomeController', ['$scope', '$http',
    function($scope, $http) {
        $http.get('/api/home.json').success(function(data) {
        $scope.posts = data;
    });
}]);

When accessing a page like /topic/topicname, I receive the correct JSON response from my API, but the data doesn't bind to the partial template.

EDIT: (json response)

[{
        "user" : "jack",
        "source_id" : "rX5n0oP",
        "topic" : "swag",
        "title" : "http://i.imgur.com/rX5n0oP.jpg",
        "url_safe_title" : "httpiimgurcomrx5n0opjpg",
        "is_NSFW" : false,
        "source_url" : "http://i.imgur.com/rX5n0oP.jpg",
        "pk" : 8,
        "source_site" : "imgur-img"
    }, {
        "user" : "jack",
        "source_id" : "QW8Sp1O",
        "topic" : "swag",
        "title" : "http://imgur.com/QW8Sp1O",
        "url_safe_title" : "httpimgurcomqw8sp1o",
        "is_NSFW" : false,
        "source_url" : "http://imgur.com/QW8Sp1O",
        "pk" : 1,
        "source_site" : "imgur-img"
    }]

Answer №1

If you're having trouble getting your templates to bind to $scope, try using static json instead of api calls. Check out this code pen for an example: http://codepen.io/anon/pen/EazprP

$scope.posts=[{
    title:"Home"
}]

It's possible that the issue lies with your json response. Can you share the json data?

Answer №2

Looks like the angular.js tags were causing issues with my django backend tags. I made a simple adjustment

$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');

to alter the search criteria for angular.js and it resolved the problem :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sinon's fakeTimers failing to trigger

I'm encountering an issue with sinon's fakeTimers while working in a setup that includes Marionette.js, underscore, and chai test runner. Strangely, when I place a breakpoint in Chrome and step through the code, my timer functions as expected. Ho ...

Tips for dynamically resetting the dataTable

When I create a datatable and add rows dynamically based on the selected option, I encounter an issue where I need to reinitialize the dataTable every time the option is changed. To address this, I have placed the reinitialization code inside the $("#selec ...

Error encountered on NodeJS server

Today marks my third day of delving into the world of Angular. I've come across a section that covers making ajax calls, but I've hit a roadblock where a tutorial instructed me to run server.js. I have successfully installed both nodejs and expre ...

Encountered an issue while trying to use Figma's spellchecker extension

Despite following the instructions in the readme carefully, I am facing difficulties running the Figma spellchecker extension. Executing npm run build goes smoothly. But when I try to run npm run spellcheck, I encounter an error message in the console: co ...

Tips for positioning a div element within the body of a webpage to maintain a predetermined height and width

Currently, I am developing a single-page application using AngularJS. I have specific routes in mind where I want to introduce new HTML templates. To accomplish this, I have created a container labeled with the ID #main positioned between two navbars (he ...

Retrieving elements from another component's scope

Struggling with displaying a D3.js graph defined in one Angularjs 1.5 component on a page that relies on a different one. I'm still learning Angular, and although I believe the solution lies within the official documentation's example of a compon ...

`The form input status color remains static and does not update`

I encountered a situation in one of my projects where I need to visually indicate if a field is correct or incorrect based on the value of another field. To better illustrate this issue, I have created an example here. The main challenge: I am struggling ...

What is the best way to transfer a mediaStream to an AngularJS directive?

My project involves an application that captures images on different pages. I have implemented a camera service that is responsible for configuring the camera and capturing the images. Naturally, users would prefer to preview the image before it is capture ...

Error: JavaScript function call did not provide a return value

Currently, I am in the process of creating a straightforward angular2/expressJS application. Within my expressJS code, I have an HTTP GET router call that retrieves all users from MongoDB and successfully returns them: app.get('/getusers', funct ...

Eliminate the empty choice for Angular when dealing with dynamic option objects

Looking for assistance with this code snippet: <select ng-model='item.data.choice' > <option ng-if='item.data.simple_allow_blank == false' ng-show='choice.name' ng-repeat='choice in item.data.simple_choices&ap ...

Experiencing trouble with detecting intersections using the Three.js raycaster

After successfully writing a code to intersect some objects, I encountered an issue when adding a canvas along with other div elements in the HTML document. Now, there seems to be no intersection on the object. You can observe this live example here. If yo ...

Add the Selected Value to the Title

Is there a way to dynamically add the selected value from each select element to its corresponding heading without requiring user interaction? I need this to happen as soon as the page loads. Here is an example in a jsfiddle https://jsfiddle.net/dqfvyvdt/2 ...

Using Material-UI to implement a Link component from the react-router library

I'm having trouble integrating the <Link/> component into my material-ui AppBar. Here is my navigation class: class Navigation extends Component { constructor(props) { super(props) } render() { var styles = { appBar: { ...

The project runs smoothly on my local host, but encounters an issue during the build process

I am currently working on a Preact-CLI project that utilizes Preact-Router. Everything functions as expected when testing on localhost, but once the project is built and deployed to production, issues arise. One particular page within the project pulls co ...

The curly braces in AngularJS are not resolving as expected, however, the ng-bind directive is functioning properly

I'm currently working on a Django application and utilizing AngularJS for my frontend. I have a straightforward piece of code that looks like this: <div class="vert-carousel" ng-controller="PrizeController"> <div class="gallery-cell" n ...

Angulating Service Testing

I am encountering an issue that I'm not sure how to resolve because I am inexperienced when it comes to testing. Currently, I am testing a service that includes the following code: import { Injectable } from '@angular/core'; import { Endpo ...

Tips for rearranging button placements by utilizing multiple owl carousels across various webpages

I have successfully implemented two owl carousels on my Shopify website, and they are both functioning properly. However, I am facing an issue with the navigation buttons on the second page. The navigation buttons seem to be picking up a style that I had i ...

Incorporating a radio button input into the parent component in a React application

The main component stores all the state data. Whenever there is a change in any input field, the function myChangeHandler is triggered and updates the state accordingly. myChangeHandler(event) { this.setState({ [event.target.name]: event.target.value }) ...

Methods for bypassing a constructor in programming

I am working on a code where I need to define a class called programmer that inherits from the employee class. The employee class constructor should have 4 parameters, and the programmer class constructor needs to have 5 parameters - 4 from the employee c ...

CSS or jQuery: Which is Better for Hiding/Showing a Div Within Another Div?

Show only class-A at the top of the page while hiding all other classes (x,x,c). Hide only class-A while showing all other classes (x,x,c). Is it possible to achieve this? <div class="x"> <div class="y"> <div class="z"&g ...