阅读javascript中的资源文件条目 - MVC应用程序 [英] Read resource file entry in javascript - MVC application
问题描述
我正在研究一个MVC应用程序,我需要读取资源文件并获取值,并将它们传递给JavaScript文件。
为此,我只是从cshtml文件中调用一个用js文件编写的js函数。该函数将具有资源文件值的参数传递给javascript中的函数。现在在js文件中,我在与上述函数相同的名称空间中有另一个函数,我需要读取这些资源文件值。
我的cshtml代码:
< ; script type =text / javascript>
Mynamespace.function2(@ Globalstrings.Value1,@ Globalstrings.Value2);
< / script>
JS文件:
var Mynamespace = {
function1:function(){
Mynamespace.function2.getMess(); //我需要在这个函数中访问value1
},
function2:function(message1,message2){
var value1 = message1;
var value2 = message2;
return {
getMess:function(){return value1;},
getLab:function(){return value2;}
}
}
}
当我尝试了以上时,我在getMess中得到了undefined。
所以,我可以有2个选项,主要可以被接受为这样的答案:
- 任何其他阅读资源的方式文件的价值在js?
- 上述js代码的任何修正
我实际上在几个小时前发布了这个问题在另一个函数中访问变量,返回undefined - JavaScript ),但它仅限于JavaScript范围问题,但后来意识到我会发布实际问题。
任何帮助将不胜感激。
在setvalues的返回对象中需要一个逗号。它仍然不起作用,但至少可以修复你的语法错误。
return {
getMess:function( ){return value1;},
getLab:function(){return value2;}
}
另外,它似乎有点奇怪返回 setvalue()
中的value1和value2,我可以建议返回getter中的值吗?修正并重构此小提琴
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:
- Any other way of reading resource file value in js?
- 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.
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
这篇关于阅读javascript中的资源文件条目 - MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!