I have the following Angular declarations, and get no complaints about JS syntax errors or anything like that:
App.js
:
(function () {
'use strict';
var hookPointApp = angular.module("hookPointApp", []);
hookPointApp.controller("agentCtrl", function ($scope, $http) {
$scope.model = {};
...
});
})();
suburbsCtrl
:
(function () {
'use strict';
angular.module("hookPointApp").controller('suburbsCtrl', function ($scope, $http) {
$scope.areas.url = "/Area/ProvinceAreas";
$scope.areas.getOptions = function (provinceId) {
var area = "{'provinceId': '" + provinceId + "'}";
$.ajax({
url: $scope.areas.url,
type: "POST",
data: area,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function (response) {
if ((response !== null) && (typeof response !== "undefined")) {
$scope.areas.options = response;
}
},
error: function (xmlHttp, textStatus, errorThrown) {
alert(errorThrown);
}
});
};
// TODO Get a proper provinceId somehow.
$scope.areas.getOptions(1);
...
});
})();
Then I have a repeater with options declared like:
<div ng-controller="suburbsCtrl">
<div class="form-group">
@Html.LabelFor(model => model.AreaId, new { @class = "control-label" })
<select class="form-control" ng-model="areas.areaId" ng-change='areas.getOptions($("#province.val()"))'>
<option> - Select an Area - </option>
<option id="province" ng-repeat="item in areas.options" ng-value="{{item.Id}}">{{item.Label}}</option>
</select>
</div>
</div>
Yet getOptions
isn't called when I change the select option, and breakpoints I put in the Chrome debugger, even on lines not in functions, execution doesn't stop there, e.g. $scope.areas.getOptions(1);
which is for initializing a dropdown, is not in a function, but below the function declaration, so I want to declare the function then call it immediately afterwards. A breakpoint in the module file, on var hookPointApp = angular.module("hookPointApp", []);
doesn't even work, yet some Angular functionality in the app still sometimes works.
The Angular (core and my own) files are, I think, correctly referenced at the bottom of the body
element in Layout.cshtml
:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script src="~/Scripts/angular/angular.js"></script>
<script src="~/Scripts/angular/angular-resource.min.js"></script>
<script src="~/Scripts/App/App.js"></script>
<script src="~/Scripts/App/suburbsCtrl.js"></script>
@RenderSection("ViewScripts", required: false)
</body>
What could I be doing wrong?