在angularjs中启用和禁用

时间:2015-10-12 13:17:24

标签: javascript angularjs

我尝试禁用第二个字段,直到我没有在第一个字段中输入'Lokesh',因为我尝试了下面的代码我正在学习angularjs的阶段所以请纠正我在哪里做错了。

eg.code

<!DOCTYPE html>
       <html>
          <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <body>

            <p>Enter 'Lokesh' to display last name</p>

                <div ng-app="myApp" ng-controller="myCtrl">



                    <input data-ng-model="firstName"  

                   type="text"  

                   ng-disabled="{if(firstName="lokesh")}"

                   Last Name: <input type="text" ng-model="lastName"><br>
                   />


                  <script>
                    var app = angular.module('myApp', []);
                    app.controller('myCtrl', function($scope) {

                    $scope.lastName= "Kumar Gaurav";
                    });

            </script>                               
    </body>

1 个答案:

答案 0 :(得分:2)

问题在于属性ng-disabled="{if(firstName=" lokesh ")}"

无需使用if,您可以直接比较字符串。

将代码更新为

ng-disabled="firstName=='lokesh'"

<强>演示

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app>
  <input data-ng-model="firstName" class="span12 editEmail" type="text" placeholder="Type lokesh here" />Last Name:
  <input type="text" ng-model="lastName" ng-disabled="firstName!=='lokesh'" />
  <br>{{firstName}}

</div>