GVKun编程网logo

javascript – ng-options,对象设置为ng-model(js设置对象的key)

16

本篇文章给大家谈谈javascript–ng-options,对象设置为ng-model,以及js设置对象的key的知识点,同时本文还将给你拓展c#–JavaScript–如何在Javascript中

本篇文章给大家谈谈javascript – ng-options,对象设置为ng-model,以及js设置对象的key的知识点,同时本文还将给你拓展c# – JavaScript – 如何在Javascript中将值设置为Session、Calling Flex / Actionscript functions from Javascript、JavaScript Scoping and Hoisting 翻译_javascript技巧、javascript select options 排序(保持option 对象完整性)等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

javascript – ng-options,对象设置为ng-model(js设置对象的key)

javascript – ng-options,对象设置为ng-model(js设置对象的key)

我有以下 HTML:

<divng-repeat="question in currentTemplate.questions">
    <divng-show="editingQuestion[$index]">
        <selectng-model="editingCurrentlySelectedAnswerOption[$index]"
        ng-options="answerOption as answerOption.back for answerOption in answerOptions">
        </select>
    </div>
</div>

我想知道如何设置select语句中显示的值.当页面加载时,它应该已经设置,因为我通过for循环来设置editingCurrentlySelectedAnswerOption的值,以便每个都被预先选择([$index]指的是ng-repeat的索引,这是在里面因为在ng-repeat中有多个选择,我需要在数组中每个ng-repeat一个点来跟踪每个单独的选择),但是它首先作为空白点出现.我使用了ng-init,一个按下按钮时调用的函数,它取消隐藏上面的div,以及页面加载时的函数.我在DOM和console.logs中使用了绑定来检查值.他们似乎都是正确的. ng-options中的answerOptions是:

var answerOptions = [
    { front: "RadioButton",back: "Multi-Choice" }
    { front: "CheckBox",back: "Multi-Answer" }
    { front: "TextBox",back: "Free Text" }
];

当我打印editCurrentlySelectedAnswerOption [$index]时,它总是正确显示一个对象,就像上面的一个对象一样,但由于某种原因,它在加载时总是一个空白的select语句.有什么我不知道ng-options如何与对象一起工作?或者我做错了什么?

更新:

我这样设置editingCurrentlySelectedAnswerOption的值:

this.scope.editingCurrentlySelectedAnswerOption = [];

for (var i in this.scope.currentTemplate.questions) {
if (this.scope.currentTemplate.questions.hasOwnProperty(i)) {
        this.scope.editingCurrentlySelectedAnswerOption.push(this.scope.currentTemplate.questions[i].answerType);
    }
}

我在this.scope.currentTemplate中增加问题的次数的原因是因为在html中ng-repeat重复了问题的数量.他们应该匹配.

这就是this.scope.currentTemplate.questions [0]的定义方式:

new Utilities.Question(
    "Question1",{ front: "TextBox",back: "Free Text" },["Yes","Maybe","No","I don't kNow"],[50.0,25.0,0.0,-25.0]
)

这是Utilities.Question的定义:

export class Question {
    question: string;
    answerType: any;
    answerOptions: string[];
    answerWeights: number[];

    constructor(question?: string,answerType?: any,answerOptions?: string[],answerWeights?: number[]) {

        this.question = question;
        this.answerType = answerType;
        this.answerOptions = answerOptions;
        this.answerWeights = answerWeights;

    }

}

解决方法

每个人都是正确的ng-model / ng-options是指对象的引用而不是值,所以即使两个对象的值是正确的,引用也不一样,因此,select没有看到ng -model变量等于给定的选项.为了解决这个问题,我刚刚在ng-options的末尾添加了一个曲目,现在可以正常工作了.不确定这是否是最佳方式,但它确实有效!谢谢大家的帮助!

c# – JavaScript – 如何在Javascript中将值设置为Session

c# – JavaScript – 如何在Javascript中将值设置为Session

我想在Session中设置Dropdown选择的值.我在Code中完成了.但是对于某些条件,我必须在客户端自己做.我尝试了以下内容.但我还没解决.

<%session["Test"] = "Welcome Mamu";%>  
var session_value='<%=Session["Test"]%>';  
alert(session_value);

以上工作很好.请注意,我已分配静态值(欢迎Mamu).但对于Dynamatic,

var strTest=document.getElementById('DropDownList1').value;  
<%session["Test"] = "'+ strTest +'";%>

它在客户端工作正常.但是我的服务器端(Code Behind),Session [“Test”]值是’strTest’.

是否有任何其他方式为Session分配值?

解决方法@H_301_23@
无法通过javascript直接分配会话值.

我找到了其他方法.调用函数后面的代码并分配会话值.

Javascript功能:

function InitializeRequest(path) 
{
            // call server side method
            PageMethods.SetDownloadpath(path);
}


[System.Web.Services.WebMethod]
public static string SetDownloadpath(string strpath)
{
    Page objp = new Page();
    objp.Session["strDwnPath"] = strpath; 
    return strpath;
}

必须启用页面方法设置为true

< asp:ScriptManager EnablePageMethods =“true”ID =“MainSM”runat =“server”ScriptMode =“Release”LoadScriptsBeforeUI =“true”>< / asp:ScriptManager>

Calling Flex / Actionscript functions from Javascript

Calling Flex / Actionscript functions from Javascript


Nowadays I am working with Flex projects,that runs on Lotus Notes platform,which consumes data from Lotus Notes backend. Since there is no remote services like BlazeDS to connect to Notes server,am Now greatly dependant on HTTPService and Javascript. Calling a javascript function from Flex is quite easy. Just use the ExternalInterface API. For those who don’t kNow the way,this is how it is getting called.

In AS3

if(ExternalInterface.available){
ExternalInterface.call(“openNotes”,parameter);
}

In Javascript

function openNotes(notesUrl){
window.open(notesUrl,”,‘width=1000,height=600′);
}

It is quite easy. But what if you need to call the reverse. ie,calling actionscript function from javascript. We can use the sameExternalInterface api for achieve this. There is a method calledaddCallback,available in ExternalInterface. addCallback method registers an ActionScript method as callable from the container. After a successful invocation of addCallBack(),the registered function in the player can be called by JavaScript or ActiveX code in the container. The first parameter is the name by which the container can invoke the function and the second parameter is the function closure to invoke. Below is the step-by-step configuration:

Step 1 : Register the call back from actionscript. For eg,call the below method in the creationComplete or initialize event of the component.

private function createCallBack(event:Event):void{
ExternalInterface.addCallback(“updateNotes”,getNotes);
}

private function getNotes():void{
//do whatever you want after getting the hold
}

Step 2 : Create logic in javascript to invoke the AS3 function.

//Javascript
function updateFlex(){
appName.updateNotes();
}

The appName is the name and id of the embedded swf object in the HTML. Like below :

That’s it. Now you can call the updateFlex javascript method from your HTML and it will invoke the AS3 callback function. Enjoy Coding guys. Flex Your Life. Cheers. :)




Reference:

http://deviltechie.wordpress.com/2012/05/08/calling-flex-actionscript-functions-from-javascript/

JavaScript Scoping and Hoisting 翻译_javascript技巧

JavaScript Scoping and Hoisting 翻译_javascript技巧

你知道下面的JavaScript代码执行后会alert出什么值吗?

复制代码 代码如下:

var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();

如果答案是"10"令你感到惊讶的话,那么下面这个会让你更加困惑:
[/code]
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
[/code]
浏览器会alert“1”。那么,到底是怎么了?尽管这看起来有点奇怪、有点危险又有点令人困惑,但这事实上却是这门语言一个强力的具有表现力的特性。我不知道是不是有个标准来定义这种行为,但是我喜欢用”hoisting”来描述。这篇文章试着去解释这种机制,但是首先,让我们对JavaScript的scoping做一些必要的了解。
Scoping in JavaScript
对于JavaScript新手来说scoping是最令人困惑的部分之一。事实上,不仅仅是新手,我遇到或很多有经验的JavaScript程序员也不能完全理解scoping。JavaScript的scoping如此复杂的原因是它看上去非常像C系语言的成员。请看下面的C程序:
复制代码 代码如下:

#include
int main() {
int x = 1;
printf("%d, ", x); // 1
if (1) {
int x = 2;
printf("%d, ", x); // 2
}
printf("%d\n", x); // 1
}

这段程序的输出是1,2,1。这是因为在C系语言有块级作用域(block-level scope),当进入到一个块时,就像if语句,在这个块级作用域中会声明新的变量,这些变量不会影响到外部作用域。但是JavaScript却不是这样。在Firebug中试试下面的代码:
复制代码 代码如下:

var x = 1;
console.log(x); // 1
if (true) {
var x = 2;
console.log(x); // 2
}
console.log(x);// 2

在这段代码中,Firebug显示1,2,2。这是因为JavaScript是函数级作用域(function-level scope)。这和C系语言是完全不同的。块,就像if语句,并不会创建一个新的作用域。只有函数才会创建新的作用域。
对于大部分熟悉C,C++,C#或是Java的程序员来说,这是意料之外并且不被待见的。幸运的是,因为JavaScript函数的灵活性,对于这个问题我们有一个解决方案。如果你必须在函数中创建一个临时的作用域,请像下面这样做:
复制代码 代码如下:

function foo() {
var x = 1;
if (x) {
(function () {
var x = 2;
// some other code
}());
}
// x is still 1.
}

这种方面确实非常灵活,它使用在任何需要创建一个临时作用域的地方,不仅仅是某个块中。但是,我强烈建议你花点时间好好理解下JavaScript scoping。它实在是非常强力,而且它也是我最喜欢的语言特性之一。如果你很好的理解了scoping,理解hoisting将会更加容易。
Declarations, Names, and Hoisting
在JavaScript中,一个作用域(scope)中的名称(name)有以下四种:
1. 语言自身定义(Language-defined): 所有的作用域默认都会包含this和arguments。
2. 函数形参(Formal parameters): 函数有名字的形参会进入到函数体的作用域中。
3. 函数声明(Function decalrations): 通过function foo() {}的形式。
4. 变量声明(Variable declarations): 通过var foo;的形式。
函数声明和变量声明总是被JavaScript解释器隐式地提升(hoist)到包含他们的作用域的最顶端。很明显的,语言自身定义和函数形参已经处于作用域顶端。这就像下面的代码:
复制代码 代码如下:

function foo() {
bar();
var x = 1;
}

实际上被解释成像下面那样:
复制代码 代码如下:

function foo() {
var x;
bar();
x = 1;
}

结果是不管声明是否被执行都没有影响。下面的两段代码是等价的:
复制代码 代码如下:

function foo() {
if (false) {
var x = 1;
}
return;
var y = 1;
}
function foo() {
var x, y;
if (false) {
x = 1;
}
return;
y = 1;
}

注意到声明的赋值部分并没有被提升(hoist)。只有声明的名称被提升了。这和函数声明不同,函数声明中,整个函数体也都会被提升。但是请记住,声明一个函数一般来说有两种方式。考虑下面的JavaScript代码:
复制代码 代码如下:

function test() {
foo(); // TypeError "foo is not a function"
bar(); // "this will run!"
var foo = function () { // 函数表达式被赋值给变量''foo''
alert("this won''t run!");
}
function bar() { // 名为''bar''的函数声明
alert("this will run!");
}
}
test();

在这里,只有函数声明的方式会连函数体一起提升,而函数表达式中只会提升名称,函数体只有在执行到赋值语句时才会被赋值。
以上就包括了所有关于提升(hoisting)的基础,看起来并没有那么复杂或是令人困惑对吧。但是,这是JavaScript,在某些特殊情况下,总会有那么一点复杂。
Name Resolution Order
需要记住的最最重要的特例就是名称解析顺序(name resolution order)。记住一个名称进入一个作用域一共有四种方式。我上面列出的顺序就是他们解析的顺序。总的来说,如果一个名称已经被定义了,他绝不会被另一个拥有不用属性的同名名称覆盖。这就意味着,函数声明比变量声明具有更高的优先级。但是这却不意味着对这个名称的赋值无效,仅仅是声明的部分会被忽略而已。但是有下面几个例外:
内置的名称arguments的行为有些怪异。他似乎是在形参之后,函数声明之前被声明。这就意味着名为arguments的形参会比内置的arguments具有更高的优先级,即使这个形参是undefined。这是一个不好的特性,不要使用arguments作为形参。
任何地方试图使用this作为一个标识都会引起语法错误,这是一个好的特性。
如果有多个同名形参,那位于列表最后的形参拥有最高的优先级,即使它是undefined。
Name Function Expressions
你可以在函数表达式中给函数定义名称,就像函数声明的语句一样。但这并不会使它成为一个函数声明,并且这个名称也不会被引入到作用域中,而且,函数体也不会被提升(hoist)。这里有一些代码可以说明我说的是什么意思:
复制代码 代码如下:

foo(); // TypeError "foo is not a function"
bar(); // valid
baz(); // TypeError "baz is not a function"
spam(); // ReferenceError "spam is not defined"
var foo = function () {}; // 匿名函数表达式(''foo''被提升)
function bar() {}; // 函数声明(''bar''和函数体被提升)
var baz = function spam() {}; // 命名函数表达式(只有''baz''被提升)
foo(); // valid
bar(); // valid
baz(); // valid
spam(); // ReferenceError "spam is not defined"

How to Code With This Knowledge
现在你明白了作用域和提升,那么这对编写JavaScript代码意味着什么呢?最重要的一条是声明变量时总是使用var语句。我强烈的建议你在每个作用域中都只在最顶端使用一个var。如果你强制自己这么做,你永远也不会被提升相关的问题困扰。尽管这么做会使的跟踪当前作用域实际声明了哪些变量变得更加困难。我建议在JSLint使用onevar选项。如果你做了所有前面的建议,你的代码看起来会是下面这样:
复制代码 代码如下:

/*jslint onevar: true [...] */
function foo(a, b, c) {
var x = 1,
bar,
baz = "something";
}

What the Standard Says
我发现直接参考ECMAScript Standard (pdf)来理解这些东西是如何运作的总是很有用。下面是关于变量声明和作用域的一段摘录(section 12.2.2):
If the variable statement occurs inside a FunctionDeclaration, the variables are defined with function-local scope in that function, as described in section 10.1.3. Otherwise, they are defined with global scope (that is, they are created as members of the global object, as described in section 10.1.3) using property attributes { DontDelete }. Variables are created when the execution scope is entered. A Block does not define a new execution scope. Only Program and FunctionDeclaration produce a new scope. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

我希望这篇文章能够给JavaScript程序员最容易困惑的部分一些启示。我尽力写的全面,以免引起更多的困惑。如果我写错了或是漏掉了某些重要的东西,请一定让我知道。

javascript select options 排序(保持option 对象完整性)

javascript select options 排序(保持option 对象完整性)


[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

您可能感兴趣的文章:
  • javascript据option的value值快速设定初始的selected选项
  • javascript Select标记中options操作方法集合
  • JavaScript Select和Option列表元素上下左右移动
  • javascript 动态设置已知select的option的value值的代码
  • js 操作select和option常用代码整理
  • javascript对select标签的控制(option选项/select)
  • JS对select控件option选项的增删改查示例代码
  • js获取select选中的option的text示例代码
  • js select option对象小结
  • JS动态添加与删除select中的Option对象(示例代码)
  • JS获取select-option-text_value的方法
  • JavaScript操作select元素和option的实例代码

关于javascript – ng-options,对象设置为ng-modeljs设置对象的key的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c# – JavaScript – 如何在Javascript中将值设置为Session、Calling Flex / Actionscript functions from Javascript、JavaScript Scoping and Hoisting 翻译_javascript技巧、javascript select options 排序(保持option 对象完整性)等相关内容,可以在本站寻找。

本文标签: