简体   繁体   English

Angular ng-controller与ng-app冲突

[英]Angular ng-controller conflicting with ng-app

I'm trying to use a custom control to get contenteditable elements in Angular as described in this example . 我正在尝试使用自定义控件来获取Angular中的contenteditable元素,如本示例中所述 However, I also need to use a controller which causes the contenteditable portion of the code to fail even if my controller is completely empty. 但是,我还需要使用一个控制器,即使我的控制器完全为空,它也会导致代码的可内容编辑部分失败。

This Plunker shows the desired behavior, but does not include the controller. 此柱塞显示所需的行为,但不包括控制器。

<body ng-app="myApp">
  <div>
    <h1 contenteditable ng-model="test">123</h1>
    <textarea ng-model="test"></textarea>
  </div>
</body>

This Plunker shows the addition of the controller to the div on line 10 of the HTML file, however the desired behavior from the first Plunker is no longer there. 该Plunker显示了将控制器添加到HTML文件的第10行上的div的功能,但是,第一个Plunker所需的行为不再存在。

<body ng-app="myApp">
  <div ng-controller="mainController">
    <h1 contenteditable ng-model="test">123</h1>
    <textarea ng-model="test"></textarea>
  </div>
</body>

You have to explicitly register a controller with the app - global controller functions are not allowed by default: 您必须在应用中显式注册控制器-默认情况下,不允许使用全局控制器功能:

.controller("mainController", function mainController($scope){

});

This is now the default option in Angular v1.3+ 现在,这是Angular v1.3 +中的默认选项

(of course, you have other issues with binding to untrusted HTML, but that's a different story) (当然,与不受信任的HTML绑定还有其他问题,但这是另一回事)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM