3

i wish to use angular in rails mounted engine. now what i added:

in master applications:

  1. ng-app to the main html tag

  2. angular.js file in assets/javascript

in the rails mounted engine:

  1. div ng-controller="Ctrl" with {{ title }}

  2. var app = angular.module('app', []).controller('Ctrl', function($scope) {
    
      $scope.title = "42";
    
    });
    

now what i got is:

  1. instead of 42 i see {{title}}

  2. when i console.log(angular) i see its defined

  3. if instead of ng-app in master application i use angular.bootstrap it doesn't work

  4. if i do angular.bootstrap after 5 seconds, it works (tried document.ready, doesn't help)

now most how'tos i saw either were not complete or required a lot of dependencies and refactoring and i hope it can be done faster.

6
  • 1
    Do you add ng-app="app"? because you should define a name. Commented May 1, 2014 at 11:39
  • thanks, but that causes an Uncaught Error: [$injector:modulerr]. i this error happens when using modules you did not include, but i just used a simple {{var}} Commented May 1, 2014 at 11:56
  • 1
    Perhaps you should inject $scope: .controller('Ctrl', ['$scope', function($scope) { $scope.title = "42"; })]; Commented May 1, 2014 at 11:57
  • 1
    Also 'use strict'; at the start of js file is recommended by ng docs, but i don't think it affects. Commented May 1, 2014 at 12:02
  • I'll post it as an answer. Commented May 1, 2014 at 12:03

1 Answer 1

2

Use $scope:

'use strict';

angular.module('app', []).controller('Ctrl', ['$scope', function($scope) {
  $scope.title = "42";
}]);

Or you can inject '$rootScope'

in template:

 <body ng-app="app">
Sign up to request clarification or add additional context in comments.

4 Comments

friend wait, i noted it doesnt work yet... using it "as is" prompts an error "Unexpected token )" so i changed closing to closing to "}] )" - no error- but doenst work
after changing to }]) no errors - but does not work as before. using rootscope also not...
Really strange. Don't know what else to recommend, do you have repository? Maybe i could notice something.
well, the git repository for entire project is company private, so cant, but we can maybe work around it: i can show your example to several people and maybe will get whats not working for us. anyways, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.