Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active August 14, 2016 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/8532d399fdc698a619ba92d6f2d01665 to your computer and use it in GitHub Desktop.
Save weshouman/8532d399fdc698a619ba92d6f2d01665 to your computer and use it in GitHub Desktop.
angular notes

Transition messages

Following code displays log messages that get fired when state transitions happen,
watch for the error messages, in case any exists, if 404 is found for a requested object
check the templateURL naming the state shows in the developer tools console

Add the following to the html were angular is activated ie: anywhere into index.html.erb given that application.html.erb has angular activated

<script>
angular.element(document).ready(function(){
// Credits: Adam's answer in http://stackoverflow.com/a/20786262/69362
// Paste this in browser's console
var $rootScope = angular.element(document.querySelectorAll("[ui-view]")[0]).injector().get('$rootScope');

$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
  console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
});

$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams){
  console.log('$stateChangeError - fired when an error occurs during transition.');
  console.log(arguments);
});

$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
  console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.');
});

$rootScope.$on('$viewContentLoaded',function(event){
  console.log('$viewContentLoaded - fired after dom rendered',event);
});

$rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){
  console.log('$stateNotFound '+unfoundState.to+'  - fired when a state cannot be found by its name.');
  console.log(unfoundState, fromState, fromParams);
});
console.log("Some testing line");
});

</script>

Making State Transitions

In the index.html (or any html file that gets rendered) add the following snippets

<div ui-view>here</div>
<a ui-sref="MainState">Click Here to goto MainState</a>
<a ui-sref="SecState">Click Here to goto SecState</a>

or

<ui-view/>
<a ui-sref="MainState">Click Here to goto MainState</a>
<a ui-sref="SecState">Click Here to goto SecState</a>

That way when we click on some Click Here text we'll get a state transition with it's log messages.

Including a file into another

That's how we can include files/nested files, If I recall right, angular-rails-templates should be working for that.

<!--
  including the 2 files, that shall work if angular-rails-templates is working properly
  file1: app/assets/javascripts/test.tmpl.html
  file2: app/assets/javascripts/templates/stern/departments/departments.tmpl.html ->-
<div ng-include='"test.tmpl.html"'></div>
<div ng-include='"stern/departments/departments.tmpl.html"'></div>

Template Urls

Represent html files with pathes relative to the directories require-tree-ed in application.js ie:

//= require_tree ./templates  
//= require_tree .  

enable us to access templates relative to:
app/assets/javascripts/templates/
and
app/assets/javascripts/
in order.

Using double quotation in single quotations syntax works with files that have no slashes in their path
ie: a file that is found directly in app/assets/javascripts/templates or app/assets/javascripts/

If you are asking Why!? the reason is that the final output is inserted into double quotations, which results into ""example.html"" instead of "example.html"

an example:

$stateProvider.state('main_state', {
  url: '/',
  templateURL: 'stern/departments/departments.tmpl.html',
  controller: 'mainController',
  controllerAs: 'mainCtrl'
})
@weshouman
Copy link
Author

after requiring javascripts and javascripts/templates

examples that worked/didn't work

      templateUrl: 'stern/departments/departments.tmpl.html', // works, file is app/assets/javascripts/stern/departments/departments.tmpl.html
      templateUrl: 'departments.tmpl.html', //won't work, file is app/assets/javascripts/templates/stern/departments/departments.tmpl.html
      templateUrl: '"test.tmpl.html"',  //works, don't do that, doesn't work with urls containing slashes
      templateUrl: 'stern/index.html.erb',  //won't work, file is app/views/stern/index.html.erb
      templateUrl: 'views/stern/index.html.erb',  //won't work file is app/views/stern/index.html.erb
      templateUrl: 'stern/test3.tmpl.html',  //works, file is app/assets/javascripts/stern/test3.tmpl.html
      templateUrl: '../stern/test3.tmpl.html', //doesn't work, file is app/assets/javascripts/stern/test3.tmpl.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment