2

first

exports.render = function(req,res){
    res.render('index', {
        title: 'welcome',
        username : req.user ? req.user :''
    });

};

I want to export object req.user to use

h2 Hello #{username.username}
h2 Hello #{username}

In Jade I can use it

div(ng-app='IndexController')
             {{ #{username} | getUserData }}

but if i want to get it to angular it can't use

mainAppModule.filter('getUserData', function () {
    return function (name) {
        if (name)
            console.log(name.username + "xxxxxxxxxxxxxxxxxxxxxxxxqqqqqqqqqq");
        return 'Hellox, ';
    };
});

How can i do it? how to get req.user for use in future.if there is other way to get object to use in angular please tell me. Thank so much.

2
  • your understanding of how expressions in angular work seems flawed. {{ #{username} }} is going to output {{someName}}, which in turn means angular will be looking for a property $scope.someName; it is not creating a property with the name as a value that could be consumed.... Commented Oct 4, 2016 at 14:18
  • if you must do this, then you should use ng-init. note that this is one of the only recommended uses for the ng-init directive. Commented Oct 4, 2016 at 14:20

2 Answers 2

1

function MyCtrl($scope) {
  $scope.req = {'user':'scope user'};  
    
   this.req1 = {'user':'alias user'} ;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
  <div ng-controller="MyCtrl as myCont">
      {{req.user}}
      <br/>
      {{myCont.req1.user}}
  </div>
</div>

I am not sure what problem you are facing. You can simply use $scope or controller alias to access any variable in the view.

By Alias

<div ng-controller="MainController as main">
    {{main.name.userName}}
</div>

By $scope

<div ng-controller="MainController">
    {{name.userName}}   
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

Seems you need to pass your request object to your Angular code.

What I can suggest is try to make a module which will act as a model for jade rendered objects as below:

index.jade

div(ng-controller="myController as ctrl")
    h2 Title: {{ ctrl.title | myFilter }}
    h2 Username: {{ ctrl.username | myFilter }}
  
script.
    angular.module('jadeData',[])
      .factory('jadeDataFactory',function(){
      
        //model service to set objects retrieved from jade
        return {
          'title': "#{title}",
          'username': "#{username}"
        };
      });
script(src="/js/app.module.js")

Once done, you can access the service model in you main module's controller

app.module.js

  angular.module("myApp",['jadeData'])
    .controller("myController",function(jadeDataFactory){
      var that = this;
      
      that.title = jadeDataFactory.title;
      that.username = jadeDataFactory.username;
      
    })
    .filter("myFilter",function(){
      return function(v){
        if(v)
          console.log('Inside filter value: ',v);
        return v+' Modified';
      }
    });

This way you can inject the jadeDataFactory to any of the required controllers/services

Explanation: actually Jade is rendered in server side, and if you write {{ #{username} | getUserData }} then renderer could not get it correctly, the better way is to store it in a service model and access it in required controller.

Comments

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.