AngularJS应用程序中身份不明的ng-controller问题

时间:2019-03-26 17:04:10

标签: angularjs angularjs-controller

我正在尝试一些初始的东西以适应AngularJS。我当前的来源是SoloLearn。我试图重新创建他们的应用程序,该应用程序在单击按钮时会向计数器添加+1。

我尝试从他们的应用程序中检查代码,并且还将他们的工作复制并粘贴到我的HTML文件中,但仍然无法正常工作,我尝试了an online tool for ascii diffs,但是我的代码应该相同我的检查员得出以下结果:

http://errors.angularjs.org/1.6.4/$controller/ctrlreg?p0=clickCounter
at angular.js:38
at angular.js:10839
at ba (angular.js:9931)
at n (angular.js:9701)
at g (angular.js:9055)
at g (angular.js:9058)
at g (angular.js:9058)
at angular.js:8920
at angular.js:1919
at m.$eval (angular.js:18161)

我检查了site,据我了解,我的代码应该仍然可以使用,但是没有用。

我的代码:

<div ng-app="MyApp" ng-controller="myCtrl">
    <button ng-click="count=count+1">Click me!</button>
    <p> {{count}} </p>
</div>
<script>
    var app = angular.module('MyApp',[])
    app.controller('myCtrl', function($scope) {
        $scope.count=0;
    });
</script>

他们的代码:

<div ng-app="myApp" ng-controller="clickCounter">
        <button ng-click="count=count+1">Click me!</button>
        <p>{{ count }}</p>
</div>
<script>
    var app = angular.module('myApp', []);
    app.controller('clickCounter', function($scope) {
        $scope.count = 0;
    });
</script>

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我已经将它们添加到了不同的插件中,都可以使用

http://plnkr.co/edit/Pk8bY3jMiRqwiyWhxEkn?p=preview

http://plnkr.co/edit/aJZTXMQ1dATZI4XUSCmc?p=preview

{
  "results": [
    {
      "engagement": {
        "id": 29090716,
        "portalId": 62515,
        "active": true,
        "createdAt": 1444223400781,
        "lastUpdated": 1444223400781,
        "createdBy": 215482,
        "modifiedBy": 215482,
        "ownerId": 70,
        "type": "NOTE",
        "timestamp": 1444223400781
      },
      "associations": {
        "contactIds": [
          247
        ],
        "companyIds": [

        ],
        "dealIds": [

        ],
        "ownerIds": [

        ],
        "workflowIds": [

        ]
      },
      "attachments": [

      ],
      "metadata": {
        "body": "This is a test note "
      }
    },
    {
      "engagement": {
        "id": 4709059,
        "portalId": 62515,
        "active": true,
        "createdAt": 1428586724779,
        "lastUpdated": 1428586724779,
        "createdBy": 215482,
        "modifiedBy": 215482,
        "ownerId": 70,
        "type": "CALL",
        "timestamp": 1428565020000
      },
      "associations": {
        "contactIds": [
          832
        ],
        "companyIds": [
          8347
        ],
        "dealIds": [

        ],
        "ownerIds": [

        ],
        "workflowIds": [

        ]
      },
      "attachments": [

      ],
      "metadata": {
        "durationMilliseconds": 24000,
        "body": "test call"
      }
    }
  ],
  "hasMore": true,
  "offset": 4623406
}

<div ng-app="myApp" ng-controller="clickCounter">
    <button ng-click="count=count+1">Click me!</button>
    <p>{{ count }}</p>

答案 1 :(得分:0)

正如@Lex所提到的,我需要为正在运行的应用程序专门定义Angular应用程序,而不是将所有应用程序都挤在同一个地方。 我发布的第一个URL上的代码现在可以正常工作。基本上将public class Rectangle { public double Width {get; set;} public double Height {get; set;} } public class RectangleViewModel : MagicViewModel<Rectangle> { public RectangleViewModel(Rectangle model) : base(model){ } } public class MagicViewModel<TModel> : INotifyPropertyChanged { protected readonly TModel _model; public MagicViewModel(TModel model) { _model = model; } // inpc implementation // what else? } public class Program { public static void Main(string[] args) { var vm = new RectangleViewModel(new Rectangle()); var calls = 0; vm.PropertyChanged += (sender, args) => calls++; vm.Height = 10; // magic happened here Debug.Assert(calls > 0); } } 标记更改为:body并命名每个div应用程序可使一切正常运行。 谢谢大家的帮助。