Read resource file entry in javascript - MVC application

前提是你 提交于 2019-12-11 12:10:01

问题


I am working on a MVC application and I need to read resource file and get values , pass them in a javascript file. For this I am just calling a js function written in separate js file, from cshtml file. This function passes parameters which have the resource file value, to the function in javascript. Now in js file, I have another function in the same namespace as my above function where I need to read these resource file values. How can I do that?

My cshtml code:

<script type="text/javascript">
Mynamespace.function2("@Globalstrings.Value1","@Globalstrings.Value2");
</script>

JS file:

var Mynamespace ={
    function1: function(){
        Mynamespace.function2.getMess();   // I need to access value1 here in this function
    },

    function2: function(message1,message2){
        var value1 = message1;
        var value2 = message2;
        return{
          getMess: function(){ return value1;},
          getLab: function() { return value2;}
        }
    }
}

When I tried the above, I am getting undefined in getMess. So, I could have 2 options primarily which can be accepted as an answer for this:

  1. Any other way of reading resource file value in js?
  2. Any fix for the above js code

I actually posted this question few hours ago (Accessing variable in another function, returns undefined - JavaScript), but made it limited to javascript scoping problem only,but then realised I will post the actual problem.

Any help would be appreciated.


回答1:


You need a comma in your return object for setvalues. It's still not going to work, but at least that fixes your syntax error.

return {
    getMess: function(){ return value1;},
    getLab: function() { return value2;}
}

Also, it seems kind of odd returning the value1 and value2 in setvalue(), could I suggest returning the values in the getter? Fixed and refactored in this Fiddle



来源:https://stackoverflow.com/questions/19454022/read-resource-file-entry-in-javascript-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!