AngularJS problem

Status
Not open for further replies.

krimhorn

Ars Legatus Legionis
39,865
Unless there's something new in a newer version of Angular you must have a module creating an Angular app and add the controller function to the controllers collection. The product controller is undefined because you're not defining an app to look at so there's no way for angular's controller factory to build the requested controller.

Code:
<html>
<head>
	<title>Product App</title>
	<script src="./Scripts/angular.js"></script>
	
</head>
<body>
<div ng-app="olephsApp" ng-strict-di>
    <div ng-controller="productController">
        <h1>{{Display}}</h1>
    </div>

	<script>
	angular.module('olephsApp', [])
      .controller('productController', ['$scope', '$http', function($scope, $http) {
        $scope.Display = "This is text from the controller to display";
      }])
	</script>
</div>

Edit:
[url=http:/civis/viewtopic.php?p=28336393#p28336393:19qs72oa said:
Oelph[/url]":19qs72oa]Stupid me, please ignore!

Ok.
 

Oelph

Ars Tribunus Militum
2,661
Yep you're right. I've been learning Angular with some of last years tutorials and they've been using a pre 1.3 version, so I was able to get away with controllers in the global scope.

I've only just seen your post but did the same thing to get it working:

Code:
<html>
<head>
    <title>Product App</title>
</head>
<body ng-app="myApp">

    <div ng-controller="productController">
        <h2>Web API + AngularJS Demo</h2>
        <p>This simple .net app has a static product model (product.cs) served up by a web api using Angular JS.</p>
        <ul>
            <li ng-repeat="x in products">
                {{ x.Name + ', ' + x.Category + ', ' + "£" + x.Price }}
            </li>
        </ul>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
    <script>

        var app = angular.module("myApp", []);

        app.controller("productController", function ($scope, $http) {
            $http.get("api/products")
           .success(function (response) { $scope.products = response; });
        });

    </script>
</body>
</html>

Thanks for your reply though. I didn't think anyone had seen it when I edited my OP :)
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=28338329#p28338329:4onumfp0 said:
Oelph[/url]":4onumfp0]Yep you're right. I've been learning Angular with some of last years tutorials and they've been using a pre 1.3 version, so I was able to get away with controllers in the global scope.
Interesting. The app that I'm working on is still on 1.0.8 (until 1.3 there really weren't any features that made it worth the rebuild necessity that was introduced with 1.1) and I wasn't aware that you could do that).

Thanks for your reply though. I didn't think anyone had seen it when I edited my OP :)
No problem. I had a few minor syntax issues in my original answer that kept it from recognizing the app so it took a bit longer than it should have to get put up in a working state.

I recognize that you're learning the platform but I highly recommend you start breaking things out into modules with separate files for controllers, directive, etc when you get a bit more comfortable with it. When I get some time at work tomorrow I'll take a look at my skeleton project and see what I can post as an example for how a module, controller, service and directive js file should look (nothing particularly special, just making use of javascript's loading and scopes). An Angular app is going to end up with a lot of js files and if the components of a module are all together it will make it very difficult to not only work on but to encapsulate functionality.

You're lucky. The Angular documentation has gotten a lot better since I started using it a year and a half ago.
 
Status
Not open for further replies.