简体   繁体   English

AngularJS-ng-model无法与bootstrap指令一起使用

[英]AngularJS - ng-model not working with bootstrap directives

I am using AngularJS for my project and I am new to it. 我正在为我的项目使用AngularJS,所以我是新手。 But I liked its features and very convenient for development as well. 但是我喜欢它的功能,并且非常方便开发。 But I came up with the following issue and didn't know to get out of it. 但是我想到了以下问题,却不知道该如何解决。

I have a multi view application. 我有一个多视图应用程序。 The following code is a part of signup view. 以下代码是注册视图的一部分。 This view gets displayed when the signup button is pressed. 按下注册按钮后,将显示此视图。 Now the issue is, in the 4th and 5th line below, I have attached a ng-model attribute to and I am able to print the number obtained using {{num}} directive. 现在的问题是,在下面的第4和第5行中,我已将ng-model属性附加到该属性,并且能够打印使用{{num}}指令获得的数字。 However, the ng-model num2 below is not getting displayed as above. 但是,下面的ng-model num2并没有如上所述显示。 All I get is the static text {{num2}} being displayed. 我得到的只是显示的静态文本{{num2}} Why is it not working like the previous case? 为什么它不像以前的情况那样起作用?

<form role='form' action='#/app/register_db' method='post'>
    <h1><small><b>Sign Up Information<b></small></h1>
    <br>
    <input type='text' ng-model='num'>
    <h1>{{num}}</h1>
    <div class='row'>
        <input type='text' ng-model='num2'>
        <h1>{{num2}}</h1>
        <div class='col-xs-5'>
            <input type="text" class='form-control' id="fn" name='firstname' ng-model='ng-firstname' placeholder="First Name">
        </div>
    </div>
...
...

I am new to AngularJS and I am very quickly grasping concepts. 我是AngularJS的新手,我很快掌握了概念。 So if I am missing something, then please guide me through the right path and help me fix this issue. 因此,如果我缺少任何东西,请引导我走正确的道路,并帮助我解决此问题。

I am using angularJS and Bootstrap CSS. 我正在使用angularJS和Bootstrap CSS。

Thanks. 谢谢。

You should get the following error message in your browser's console: 您应该在浏览器的控制台中收到以下错误消息:

[ngModel:nonassign] Expression 'ng-firstname' is non-assignable. Element: <input type="text" class="form-control" id="fn" name="firstname" ng-model="ng-firstname" placeholder="First Name">

As ng-model="ng-firstname" is not a reference by name, but an expression AngularJS will try to evaluate, so simply not using a dash will fix that. 由于ng-model="ng-firstname"并不是按名称的引用,而是AngularJS会尝试求值的表达式,因此不使用破折号即可解决该问题。 What happens when you break the code there is AngularJS basically stops, and anything else AngularJS would usually do in elements that follow, simply doesn't happen. 当您破坏代码时会发生什么,AngularJS基本上会停止,而AngularJS在随后的元素中通常会执行的其他任何操作,根本不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM