Troubleshooting dynamic ng-src not functioning properly with Angular JS version 1.2.0-rc.2

I'm currently facing an issue while trying to incorporate a video element into my Angular JS app. The ng-src is not recognizing the scope variable that I have set.

The version of AngularJS I am using is 1.2.0-rc.2

<!DOCTYPE html>
<html ng-app="ngView">

<head>
   <script src="http://code.angularjs.org/1.2.0-rc.2/angular.min.js"></script>

   <script>
   var app = angular.module('ngView', []);
   function MyControl($scope){
      $scope.file = '1234.mp4';
   }
  </script>
  </head>
  <body ng-controller="MyControl">
      <video controls  ng-src="http://www.thebigdot.com/{{file}}"></video>
  </body>
</html>

Interestingly, if I switch to an older version of AngularJS library like cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js, it works perfectly fine.

So, I’m left wondering whether this is a bug in the latest release or if there has been a deliberate change made to disable this feature. Any ideas on what could be causing this and how to work around it?

Answer №1

Angular 1.2 comes with Strict Contextual Escaping (SCE) enabled by default. Making it work requires a minor adjustment to your code.

HTML

To make ng-src bind to a variable instead of a URL + variable, modify the markup as follows:

<video controls ng-src="{{videoSource}}"></video>

JavaScript

Inject the SCE provider using $sce and utilize the $sce.trustAsResourceUrl method to set the videoSource.

function MyController($scope, $sce) {
    var videoSource = 'http://www.example.com/1234.mp4';
    $scope.videoSource = $sce.trustAsResourceUrl(videoSource);
}

Check out this JSFiddle demo for a demonstration of this implementation.

Answer №2

Upon thorough investigation, I discovered the root of the issue:

Error: [$interpolate:noconcat] An error occurred while interpolating: http://www.thebigdot.com/{{file}} Strict Contextual Escaping prohibits concatenating multiple expressions when a trusted value is needed.  For more information, refer to [http://docs.angularjs.org/api/ng.$sce][1] 

http://errors.angularjs.org/1.2.0-rc.2/$interpolate/noconcat?p0=http%3A%2F%2Fwww.thebigdot.com%2F%7B%7Bfile%7D%7D
    at http://code.angularjs.org/1.2.0-rc.2/angular.js:78:12
    at $interpolate (http://code.angularjs.org/1.2.0-rc.2/angular.js:6953:17)
    at attrInterpolateLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:5367:27)
    at nodeLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:5121:13)
    at compositeLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:4640:15)
    at nodeLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:5115:24)
    at compositeLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:4640:15)
    at compositeLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:4643:13)
    at publicLinkFn (http://code.angularjs.org/1.2.0-rc.2/angular.js:4549:30)
    at http://code.angularjs.org/1.2.0-rc.2/angular.js:1157:27 <video controls="" ng-src="http://www.thebigdot.com/{{file}}"> angular.js:7861

This informative piece delves into the issue at hand and provides guidance on how to disable the Strict Contextual Escaping feature: http://docs.angularjs.org/api/ng.$sce

Answer №3

This is a directive that I created

app.directive('loadAudio', function($parse) {
  return {
    restrict: 'EA',
    scope: {
        source: '=',       
    },
    template: '<audio />',
    link: function(scope, iElement, iAttrs) {

      scope.$watch('source', function(value) {
        var audio = iElement.find('audio');

        audio.attr('src',  value);
      }, true);
    }
  }
});

Below is how to use this directive in the template

<load-audio source="your_src" ></load-audio>

Answer №4

Before stumbling upon the previous post, I managed to pull off a simple hack that might be helpful for someone else.

Here's the HTML snippet:

<div id="videoTag"></div>

And here's the code from Controller.js:

document.getElementById("videoTag").innerHTML = "<video width='auto' height='auto' controls autoplay src=" + $scope.details.preview + ">Your browser does not support video</video>";

Answer №5

Switching from version 1.5.0 of angular to version 1.4.8 resolved the issue I was experiencing.

.controller('MainCtrl',['$scope','$stateParams','DepartementFactory',
        function($scope,$stateParams,DepartementFactory) {
            $scope.currentDate=new Date();
            DepartementFactory.getDepartements().then(function(data){
               $scope.departements=data;
               $scope.departements.logo="work"; 
            });
        }
    ]
)
<div class="work">
    <a>
        <img src="images/{{departements.logo}}" class="media" alt=""/>
        <div class="caption">
            <div class="work_title">
                <h1>{{departements.libelle}}</h1>
            </div>
        </div>
    </a>
</div>

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

Retrieving the dimensions of an iframe using JavaScript

I currently have an iframe on my page with a height of 0px and width of 100px that I would like to access using JavaScript. Although I can grab the iframe element using window.frames[0], I'm struggling to retrieve the actual width and height values. ...

Issue: The value of an object is not defined (evaluating '$scope.inputcode.password')

HTML Form: <form ng-submit="mylogin()"> <div class="list"> <label class="item item-input"> <span class="input-label">Username</span> <input type="text" ng-model="inputcode.username"> ...

Using the Selenium webdriver to reach the bottom of the page by scrolling vertically

Is there a way to determine if I have scrolled to the bottom of the page vertically? I have been using Webdriver and pressing the page down key repeatedly within a for loop, like so: for(int di=0; di<40; di++) { driver.findElement(By.tagName("body ...

Convert the entirety of this function into jQuery

Can someone please help me convert this code to jQuery? I have a section of jQuery code, but I'm struggling with writing the rest! function showUser(str) { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { ...

Minimize duplication of HTML code by refraining from PHP usage

It seems like this task may be simple for some individuals. To prevent redundancy in code across multiple pages, my goal is to insert segments of HTML code without having to manually input all of the elements each time. It's similar to a page controll ...

Finding the latitude and longitude coordinates of a point at a specific distance from another using Node.js or JavaScript

My task involves determining the square area based on a latitude and longitude (x,y) as shown in the provided figure. https://i.sstatic.net/Rt7mC.png To accomplish this, I must calculate the coordinates of the remaining three corners by adding 10 kilomet ...

Discover the method for creating internal links on a single page with AngularJS

What is the best way to create internal links using angular js? In html, we typically use <a href="#top">Top</a> to navigate to an element with the id "top". ...

The Art of Div Switching: Unveiling the Strategies

I have a question regarding my website. I have been working on it for some time now, but I have encountered a challenge that I am struggling to overcome. After much consideration, I am unsure of the best approach to take. The issue at hand is that I have ...

An issue arises when attempting to utilize v-model with a file input

Is there a way to reset a file input field in Vue.js after uploading a file? I attempted to set the v-model value to null, but encountered an error message that said: File inputs are read only. Use a v-on:change listener instead. This is my current cod ...

Transmit control statuses to the framework

I have a set of UI Controls that inherit from an abstract control. Within the abstract control, there is a method called isValid which every control must extend in order to return true or false. As a framework, I need to determine whether to mark the con ...

Is there a way to display two words side by side in React components?

I previously had the following code: projectName: project.get('name') === 'default' ? 'No Project' : project.get('name') In the render() method, it was written like this: <div className='c-card__projects&ap ...

Tips for creating a basic 2D grid using PaperJS without interactive features

I have developed an interactive application mockup using PaperJS, but there is a small feature missing. I am looking to create a 2D grid for user interactions when dragging objects on the screen. The grid should be static and act as guides for users. Howe ...

Display an alert when no matches are found in autocomplete suggestions

I am implementing the code below to populate a textbox with data. If I input a, all records starting with a are displayed in the dropdown from the database. However, if I input a value that does not exist in the database, there is no message like "No Recor ...

Injecting controllers and classes dynamically using AngularJS

I'm currently working on a dynamic widgets list where each widget is defined by its html, class, controller, and name. Some of these parameters may be empty based on the type of widget. These widgets are then loaded dynamically into <li> element ...

tips for dealing with login pop-ups using Python's Selenium

When using selenium, I am facing a challenge of handling a Chrome popup on a virtual machine, even when the remote desktop connection is closed. https://i.sstatic.net/ep1s9.png I have successfully managed to automatically fill in the credentials in Chrom ...

Error: Attempting to assign a value to the 'fillStyle' property of a null object in HTML5 Javascript

I've been experimenting with HTML5 canvas and JavaScript to draw shapes. While I can successfully draw shapes directly within the HTML file, I encounter an Uncaught Type Error when attempting to do so through an external JavaScript file. My goal is si ...

Exploring the outer scope within JavaScript

Currently, I am working on a JavaScript code snippet where I am attempting to set the 'obj' variable from the success and error callbacks. However, it seems like the scope of the 'toLinkInfo' function is not encompassing these callbacks ...

Adding an item to an object array in MongoDB can be achieved with the use of either the addToSet or push operators

I have a set of reviews in an array, and I am trying to implement addToSet functionality to add a review while ensuring that a user can only review once. Below is how my schema is structured: const sellerSchema = new mongoose.Schema({ user: { type: ...

AngularJS's $http.get function has the ability to read text files, but it struggles with reading JSON

I'm new to angularjs and I am struggling to retrieve data from a json file using the $http.get method. It seems to work fine when I try to read a simple txt file, but not with the json file. I can't seem to pinpoint where the issue lies... Belo ...

Tips for making a dropdown menu within an Ionic Side menu

Looking for guidance on creating a sub menu in the ionic framework? I'm new to AngularJS and ionic framework, but eager to learn. Below is the code snippet I used to create a dropdown list component: <ion-side-menu side="left"> <ion-co ...