AngularJS Controller无法使用简单的初始化代码

时间:2017-03-13 05:04:01

标签: angularjs

AngularJS的新手,并且不知道在这里发生了什么,当我运行时没有任何东西被填充....

使用版本 - 1.6 请看看,可能是非常愚蠢的问题,但没有通过;)

<!DOCTYPE html>
<html data-ng-app="">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<div data-ng-controller="simpleController">
		Name:
		<br/>
		<input type="text" data-ng-model="fname" />
		<br/>
		<ul>
			<li data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{ cust.name | uppercase }} - {{ cust.city | lowercase}}</li>
		</ul>
	</div>
<script src="js/angular.min.js"></script>
<script>
 	function simpleController($scope){
		
		$scope.customers = [
		                    {name:'anil', city:'bengaluru'},
		                    {name:'rahul', city:'pune'},
		                    {name:'abc', city:'hyd'},
		                    {name:'xyz', city:'mysore'}
		                    ];
	}

3 个答案:

答案 0 :(得分:0)

angular创建控制器,然后使用它

代码是:

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

<head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>

<body>
    <div data-ng-controller="simpleController">
        Name:
        <br/>
        <input type="text" data-ng-model="fname" />
        <br/>
        <ul>
            <li data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{ cust.name | uppercase }} - {{ cust.city | lowercase}}</li>
        </ul>
    </div>
    <script src="js/angular.min.js"></script>
    <script>
    angular.module("app").controller('simpleController', simpleController);

    function simpleController($scope) {

        $scope.customers = [{
            name: 'anil',
            city: 'bengaluru'
        }, {
            name: 'rahul',
            city: 'pune'
        }, {
            name: 'abc',
            city: 'hyd'
        }, {
            name: 'xyz',
            city: 'mysore'
        }];
    }
    </script>
</body>
</html>

答案 1 :(得分:0)

您必须在使用之前创建控制器。 通过以下方式创建控制器:

const c = {x: 'yolo', y: 'rolo', z: 'cholo'};

根据你的代码:

&#13;
&#13;
angular.Module("ModuleName",[]).Controller("ControllerName",function($scope){
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

首先,您需要定义一个模块来启动项目。Try this blog to learn more。有关更简单的示例,请尝试this

要使您的应用有效,您需要将ng-app封装在您的情况下。然后你需要在你的情况下创建一个module它缺少。然后根据您的需要添加servicescontroller

 angular.module("app",[])
  .controller("ctrl",simpleController);
 
 	function simpleController($scope){
		
		$scope.customers = [
	                    {name:'anil', city:'bengaluru'},
	                    {name:'rahul', city:'pune'},
	                    {name:'abc', city:'hyd'},
	                    {name:'xyz', city:'mysore'}
	                    ];
	}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="app" data-ng-controller="simpleController">
		Name:
		<br/>
		<input type="text" data-ng-model="fname" />
		<br/>
		<ul>
			<li data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{ cust.name | uppercase }} - {{ cust.city | lowercase}}</li>
		</ul>
	</div>