这是我用angularjs编写的第一个代码,我完全不知道我做错了什么。
我创建了这个:
var app = angular.module('koloApp', []);
app.controller('circleController', ['$scope', function ($scope) {
$scope.kolor1 = 'red';
$scope.kolor2 = 'yellow';
}]);'并使用此html:
<!DOCTYPE html>
<html lang="pl" ng-app="koloApp">
<head>
<title>KOLO</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="app/app.js"></script>
<script type="text/javascript" src="js/dyrektywy/kolo/kolo.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div>
<kolo-directive ng-click="clickMe()" kolor-wypelnienia ="'navy'" kolor-otoczenia="'yellow'"></kolo-directive>
</div>
</body>
</html>和这个指令:
var app = angular.module('koloApp', []);
app.directive('koloDirective', function () {
return {
restrict: 'E',
templateUrl: 'js/dyrektywy/kolo/kolo.html',
transclude: true,
link: function ($scope, element, attr) {
$scope.clickMe = function () {
alert("kliknąłeś w kółeczko");
}
},
scope:
{
kolorKola: '=kolorWypelnienia',
kolorObwodki:'=kolorOtoczenia'
},
};
});一切都很好。但是,当我想在html中使用ng-controller时,它完全不起作用。我试着这样使用它:<div ng-controller: "circleController">,在控制台中我得到了这个错误:http://errors.angularjs.org/1.5.7/ng/areq?p0=circleController&p1=not%20a%20function%2C%20got%20undefined
我不知道这个控制器出了什么问题。有什么想法吗?
发布于 2016-10-12 01:09:11
将冒号替换为等号。
它应该是:
<div ng-controller = "circleController">发布于 2016-10-12 01:12:36
您尚未在HTML中提供控制器。
您必须提供如下内容
<div ng-controller= "circleController"> 而不是
<div ng-controller: "circleController"> 检查以下内容:
HTML
<div ng-app="koloApp">
<div ng-controller="circleController">
<kolo-directive ng-click="clickMe()" kolor-wypelnienia ="'navy'" kolor-otoczenia="'yellow'"></kolo-directive>
</div>
</div>JS
var app = angular.module('koloApp', []);
app.controller('circleController', ['$scope', function ($scope) {
$scope.kolor1 = 'red';
$scope.kolor2 = 'yellow';
}]);
app.directive('koloDirective', function () {
return {
restrict: 'E',
template: '<h1>Changed your <b>templateUrl</b> to <b>template</b> for showing this example</h1>',
transclude: true,
link: function ($scope, element, attr) {
$scope.clickMe = function () {
alert("kliknąłeś w kółeczko");
}
},
scope:
{
kolorKola: '=kolorWypelnienia',
kolorObwodki:'=kolorOtoczenia'
},
};
});检查这个小提琴:Fiddle
https://stackoverflow.com/questions/39982792
复制相似问题