无法确定ng-change运行时错误的来源

时间:2014-07-03 05:43:23

标签: angularjs

当我运行以下代码时,我在运行时遇到编译错误,但我似乎无法弄清楚出了什么问题。问题与我使用ng-change的方式有关但我无法看到问题所在:

<!doctype html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js" ></script>
  </head>
    <script type="text/javascript">
    var myApp = angular.module('myApp', []);

    myApp.controller('MainCtrl', ['$scope', function ($scope) {

        $scope.main = {};

        $scope.doSomething = function()
        {
          var x = 1;
        };

        // No data
        $scope.main.test1 = [];

        // We have data!
        $scope.main.test2 = [{
            "some": "data"
        }];

    }]);

  </script>
  <body>
    <div ng-app="myApp">
      <div ng-controller="MainCtrl">
        <p>Test 1: {{ main.test1.length > 0 && 'My data' || 'No data' }}</p>
        <p>Test 2: {{ main.test2.length > 0 && 'My data' || 'No data' }}</p>
        <p>Test 3: {{ main.test2.length > 0 ? 'My data' : 'No data' }}</p>
        <div ng-init="scope = { isDisabled: true }">

        <input type="checkbox" ng-change="doSomething()"  />
        <button ng-disabled="{{scope.isDisabled}}">Disabled</button>
        <details open>
        <summary>Copyright 1999-2014.</summary>
        <p> - by Refsnes Data. All Rights Reserved.</p>
        <p>All content and graphics on this web site are the property of the company Refsnes Data.</p>
        </details>
</div>
      </div>
    </div>
  </body>

</html>

在jsFiddle中: http://jsfiddle.net/FS6z9/

1 个答案:

答案 0 :(得分:2)

问题出现在错误信息中......

  

指令&#39; ngChange&#39;所要求的控制器&#39; ngModel&#39;,不能成为   发现!

您不能在没有ng-change的元素上使用ng-model。它需要能够注意变化的东西,而你却没有给它任何工作。

相关问题