最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs ngDisabled comparison expression not evaluating correctly - Stack Overflow

programmeradmin2浏览0评论

I am attempting to have a button be enabled or disabled in an angularjs app based on whether a comparison of two text fields evaluates to true or false. I have provided example code below and also made it available in a plunker here

As you can see when you input a string to match the stored string the expression evaluates correctly however the button never becomes available.

Any help would be appreciated.

Here is the HTML

    <!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src=".2.16/angular.min.js" data-semver="1.2.16"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <button ng-click="updateCounter()">Increment count</button>
    <input type="text" ng-model="inputfield">
    <input type="button" value="Continue" ng-disabled="{{inputfield !== startertext}}">
    <br>startertext: {{startertext}}
    <br>nputfield: {{inputfield}}
    <br>test: {{inputfield !== startertext}}

  </body>


</html>

And the Javascript file is below.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.startertext = 'hello world';
});

I am attempting to have a button be enabled or disabled in an angularjs app based on whether a comparison of two text fields evaluates to true or false. I have provided example code below and also made it available in a plunker here http://plnkr.co/edit/rzly8hy21048YGzsx2gW?p=preview

As you can see when you input a string to match the stored string the expression evaluates correctly however the button never becomes available.

Any help would be appreciated.

Here is the HTML

    <!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js" data-semver="1.2.16"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <button ng-click="updateCounter()">Increment count</button>
    <input type="text" ng-model="inputfield">
    <input type="button" value="Continue" ng-disabled="{{inputfield !== startertext}}">
    <br>startertext: {{startertext}}
    <br>nputfield: {{inputfield}}
    <br>test: {{inputfield !== startertext}}

  </body>


</html>

And the Javascript file is below.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.startertext = 'hello world';
});
Share Improve this question asked Jun 14, 2014 at 20:29 Simon McCliveSimon McClive 2,6163 gold badges18 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

Remove the curlies around your ng-disabled attribute.

Here's the way it worked for your plunk:

<input type="button" value="Continue" ng-disabled="startertext != inputfield">

Don't forget to remove the extra curlie in your controller (marked by an error sign when I opened it in the editor).

发布评论

评论列表(0)

  1. 暂无评论