Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:40:22+00:00 2026-06-15T17:40:22+00:00

We have a directive that has an optional attribute on it. If the attribute

  • 0

We have a directive that has an optional attribute on it. If the attribute is not there, it provides a default value. The attribute is usually set from data in the scope (i.e., the value of the attribute is usually an expression and not a literal string. See http://jsfiddle.net/8PGZ4/

As such, we are using attrs.$observe in the directive to set up the scope properly. This works great in the app itself. However, when trying to test this (using Jasmine), the function in the $observe never gets run. Our test looks something like this:

describe("myDirective", function(){ 
    function getDirectiveScope(compile, rootScope, directiveHTML)
    {
        return (compile(directiveHTML)(rootScope)).scope();
    }

    describe("foo", function () {
        it("should return the default value", inject(function ($compile, $rootScope) {
            var directiveScope = getDirectiveScope($compile, $rootScope, '<div my-directive></div>');
            expect(directiveScope.bar).toBe("No Value");
        }));
        it("should now return the value given", inject(function ($compile, $rootScope) {
            $rootScope.foo = "asdf";
            var directiveScope = getDirectiveScope($compile, $rootScope, '<div my-directive foo="{{foo}}"></div>');
            expect(directiveScope.bar).toBe("asdf");
        }));
    });
});

These then fail with the following errors:

Expected undefined to be 'No Value'.
Expected undefined to be 'asdf'.

I put a console.log in the $observe function to try to see what was going on, and when the tests run, I never see the log. Is there an explicit call I have to make for the $observe to run? Is there something else going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:40:22+00:00Added an answer on June 15, 2026 at 5:40 pm

    You need to trigger a digest cycle to let the AngularJS magic happen in your test. Try:

    it("should return the default value", inject(function ($compile, $rootScope) {
      var directiveScope = getDirectiveScope($compile, $rootScope, '<div my-directive></div>');
      $rootScope.$digest();
      expect(directiveScope.bar).toBe("No Value");
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an aspx page that has a @Page directive which points to a
I have an IDL file that has a #pragma prefix directive, but whenever I
I have a view partial nameplate.jade that has a couple attributes bound from a
There is a project that has #preprocess directives. I.e. there is #preprocess directive in
I have a subprogram in PL/SQL that has PRAGMA AUTONOMOUS_TRANSACTION directive. It seems like
In the page directive of my page I have set EnableViewState=false. But I am
Can I set the max_input_vars PHP.ini directive in my code? I have it set
I have a custom home replacement App, that will not be published on the
I'm parsing RTF 1.5+ files generated by Word 2003+ that may have content from
I'm trying to write a directive that has a template. The template is rendering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.