无法获得ng-if包含的输入值

时间:2015-01-18 04:27:15

标签: javascript angularjs angular-ng-if

我无法获得ng-if包含的输入值 这是html代码

<div ng-controller="Home">
    <form ng-submit="startChat(visitor)" >
        <div class="lkv_getting">
            <div ng-if="formsetting.name == true">
                <input required ng-class="lkv_name_class" ng-model="visitor.name" id="lkv_name" name="lkv_name" type="text" class="lkv_input" placeholder="Nhập họ tên" />
            </div>                
            <textarea required maxlength="100" placeholder="Gõ tin nhắn!!" ng-model="visitor.msg" id="message_input" name="lkv_message_input"></textarea>
            <button class="lkv_button" type="submit" id="lkv_start" title="Message">Bắt đầu</button>
        </div>
    </form>
</div>

和控制器

$scope.startChat = function (visitor) {        

    var user = visitor.name; // the value is undefine
    var msg = visitor.msg; // but this one has value
};

任何人都可以帮助我吗?提前致谢

1 个答案:

答案 0 :(得分:1)

使用ng时 - 如果formsetting.name永远等于false,则DOM元素不会被渲染。这意味着您无法获得输入值,因为它们无法呈现给DOM。检查formsetting.name是否确实为true或在控制器中初始化为true,如果这对你有意义的话。如果formsetting.name可以更改值,则可能需要使用ng-show。使用ng-show将元素绘制到DOM但隐藏它们,以便您仍然可以访问它们。