Live preview feature with brackets and angularjs: Even the most basic code fails to execute

Hello, I am new to AngularJS and I am struggling to get a simple thing to work properly.

Below is the HTML:

<!DOCTYPE html>
<html>
  <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
        <script src="main.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <link rel="stylesheet" href="main.css">
  </head>
  <body ng-app="app" ng-strict-di>
    <div class="test" ng-controller="testSrc">
      <p> {{ blah }} </p>
      <img ng-src="{{ URLImage }}"/>
      <button class="btn btn-sucess" ng-click="goYahoo()">Yahoo</button>
      <button class="btn btn-sucess" ng-click="goGoogle()">Google</button>      
    </div>
  </body>
</html>

And here is the JS:

var app = angular.module("app", []);

app.controller('testSrc', ['$scope,$location', function ($scope, $location) {
  "use strict";
  $scope.blah = "blah blah";
  $scope.URLImage = 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/512px-Google_%22G%22_Logo.svg.png';

  $scope.goGoogle = function () { $location.url('localhost:58164/g'); };
  $scope.goYahoo = function () { $location.url('localhost:58164/y'); };
}]);

app.config(function ($routeProvider) {
  "use strict";
  $routeProvider
  .when('/', {
    templateUrl : 'index.html'
  })
  .when('/g', {
    templateUrl : 'http://www.google.com'
  })
  .when('/y', {
    templateUrl : 'http://www.yahoo.com'
  });
});    

I have made sure that my code passes all lint warnings. However, when I use live preview in brackets to open Internet Explorer, I encounter a few issues:

1) The expressions like {{ blah }} are not being translated into actual text and the image specified in ng-src is not displayed on the page.

2) The buttons should be colored green based on the Bootstrap CSS, but they appear as regular buttons. This was working fine in jsFiddle before but now it's not.

3) When trying to navigate to Google or Yahoo by clicking the buttons, the routing does not seem to work correctly. The URL shown in the preview is and adding /g or /y doesn't take me to the desired pages.

I'm feeling quite lost with these issues. I'm not sure if the problem lies in my code, the browser used for live preview, or some other factor. Even testing in jsFiddle did not yield the expected results...

Answer №1

1) Make sure to correctly inject dependencies into your controller by providing a string for each argument in the function.

app.controller('testSrc', ['$scope,$location', function ($scope, $location) { // Incorrect
app.controller('testSrc', ['$scope', '$location', function ($scope, $location) { // Correct

2) Check for any spelling errors in your code, such as 'sucess' instead of 'success' in class names.

 <button class="btn btn-sucess" ng-click="goYahoo()">Yahoo</button>
                        ^^^^^^

3) Identify and fix issues in your routing setup:

  • Ensure you have included the ngRoute module in your HTML, as it is no longer part of the base Angular package.
  • Add the ngRoute module as a dependency:
    var app = angular.module("app", ["ngRoute"]);
    and include an ng-view tag in your HTML.
  • By default, the router uses 'hashbangs' in the URL. If this is not suitable, consider using HTML5 mode.

Keep in mind that ngRoute may not be the best solution if you are trying to load content from external sites, as it is primarily meant for internal routing within your app.

For more guidance, consider going through the official Angular tutorial. You may also find the Shaping Up With Angular.js course on Code School helpful for hands-on learning.

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

Menu that adjusts to fit your browsing needs

Struggling to implement a fixed bottom menu on my page. Can anyone help with making it responsive? I've attempted to create the menu, but I'm having trouble linking the logo and menu items together. HTML <header class="header"> ...

Customize printing options by hiding the print button

I am looking to exclude the print button from a hard copy when it is printed. <html> <head> <title>New Page 1</title> </head> <body> <p> This is just a paragraph, and I do not want t ...

What's the reason behind beforeunload not triggering?

I've gone through numerous similar posts, but I'm struggling to figure out what's not quite right with this code snippet: $(window).on("beforeunload", function () { debugger handleBeforeUnload(); return false; }); function handl ...

Combining Different Fonts in a Single CSS File

Is there a way to incorporate multiple fonts in a CSS file? I attempted the following method, but it's not producing the desired outcome. @font-face { font-family: 'Inspira_Reg'; src: url('http://myfonturl.com'); } @font- ...

Unable to load Google charts within an Angular application

Here is an excerpt from the controller for the page I am loading, written in coffeescript. When data is received from the server, it is transformed into a json array of arrays. The method onChartDataReady is then called, which loads 3 files via the jsapi w ...

Leveraging multiple css classes within a single class definition

I need some help with this issue, it seems pretty basic but I can't seem to find the solution...sorry for asking a beginner question. Currently, I have multiple HTML elements that share the same CSS classes: <a class="class1 class2 class3">< ...

Utilizing Javascript to Extract Data from Twitter Json Files

Can someone provide assistance with parsing JSON feed text retrieved from Twitter? I am looking to access and apply style tags to elements like the link, created date, and other information. Any tips on how I can achieve this task successfully would be g ...

Scrolling within ion-content is fluid when using a fixed div in Ionic

Following the recent GoogleChrome update, a div set to 'fixed' is now scrolling with the rest of the page. For a demonstration, you can visit the following link: LINK Specifically, the issue occurs with a 'fixed' div placed inside the ...

AngularJS uses two ng-repeat directives to manage and manipulate data in

I'm implementing a feature where I have two views in my HTML5 code that display the same list. <div class="list" data-ng-repeat="item in model.items"> <div class=list-item"> {{ item.name }} <a data-ng-click="addToLi ...

Using shifting label field (in CSS) without the need for a required attribute

My challenge involves creating a floating label for a styled input using only CSS. The issue arises when I realize that this implementation requires the input to have the required attribute in order for the label to function properly (using pure CSS). Is t ...

Adjust image size according to the browser's window resize using JQuery

I am trying to make an image resize based on the browser size changes using JQuery. My goal is for the image to scale without losing its aspect ratio or getting cropped. I have incorporated Bootstrap in my HTML structure: <div class="container-fluid" ...

Modifying Line Colors in Angular-charts.js: A Step-by-Step Guide

I have different colors specified in my scope: $scope.colors = [{ // light grey backgroundColor: 'rgba(148,159,177,0.2)', pointBackgroundColor: 'rgba(148,159,177,1)', pointHoverBackgroundColor: 'rgba(148,1 ...

Tips for verifying the screen.width condition with karma and jasmine

I have been facing a challenge testing a basic if statement in my controller recently. if (screen.width <= 768) { $location.hash('map'); $anchorScroll(); } I attempted to adjust the browser size but unfortunately, the ...

What is the best way to pass ng-model value from directive to controller in Angular?

I recently developed a custom directive which includes a text box. I am facing difficulties in retrieving the value of the model associated with it in my controller. Despite my efforts, I have not been successful. Below is the code for my directive: app. ...

Is it possible to utilize :not in this scenario?

<ul class="promocode"> <li>one</li> <li>two</li> <li class="three">three</li> </ul> Is there a way to use :not in order to apply a hover effect to the entire list, except when hovering ov ...

Starting Out with Typescript Types - Unidentified term 'angular'

We are currently working on revitalizing an older AngularJS project by migrating it to .Net Core 3.1. As part of this process, we are looking to transition our scripts to TypeScript. While the project is functioning properly without TypeScript, we believe ...

mandatory data fields for an HTML form

I'm having some trouble creating an HTML form with mandatory input fields. The code I have so far is shown below: <div class="col-sm-6"> <label for="city" class="form-control">City ...

eliminate odd gaps

Query: Is there a way to remove the space shown in the first image? Additional Information and Code: I am working on displaying a list of contacts one above the other. When a user hovers over a contact's name, a contact card appears with an email i ...

When trying to show a Vue view, there was an issue with reading properties of null, specifically the 'style' property

I am experiencing an issue with a header that has an @click event in the body. Instead of displaying a Vue view upon clicking, I am encountering the following error: Cannot read properties of null (reading 'style') Upon researching on Stack Ove ...

Ways to correct the inaccurate address information filled in the text input field following a selection from Google Places autocomplete

While utilizing the Google Places API with autocomplete to create a form for users to input an address, I encountered an issue. The autocomplete feature displayed a list of addresses, and upon selecting one, the address filled into the input text was incor ...