ng-show基于其他一些html元素的模型值

时间:2016-04-12 05:42:02

标签: javascript angularjs ng-show

我有一个简单的Angular程序,只有当输入值为'Peter'时才能显示范围。我希望如果我改变输入值,那么span必须消失,但是当我试图改变输入框的值时,它不允许我这样做。由于我无法改变输入值,问题是什么?

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> 
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <input type="text" ng-model="name"/>
    <span ng-show="name='peter'">{{name}}</span>
    <script>
    //module declaration
    var app = angular.module('myApp', []);
    //controller declaration
    app.controller('myCtrl',function($scope){
        $scope.name = "Peter";

    });
    </script> 
</body> 
</html> 

2 个答案:

答案 0 :(得分:2)

ng-show更改为

<span ng-show="name.toLowerCase()==='peter'">{{name}}</span>

您正在使用赋值运算符,但您需要比较这些值并更改输入值的大小写。

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="myApp">
  <head>
  <script data-require="angular.js@1.4.8" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
  <script src="app.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <input type="text" ng-model="name"/>
    <span ng-show="name.toLowerCase()==='peter'">{{name}}</span>
    <script>
    //module declaration
    var app = angular.module('myApp', []);
    //controller declaration
    app.controller('myCtrl',function($scope){
        $scope.name = "Peter";

    });
    </script> 
</body> 

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个。

o_is_follow

20120