Issue
I am new to Angular JS and I tried to load this JS in one of the files (cshtml extension), it did alert the message 'testing 1' but it did not alert 'testing 2'. Can I know whats the issue here. Your kind guidance would be a great help to me. From what I understand is that when the page loads, it will run the $q.all correct ? But somehow, it's not working
var app = angular.module('myApp');
alert("Testing 1");
app.controller('ListController', function ($scope, $q, $http,$timeout) {
function doTask1() {
var deferred = $q.defer();
deferred.resolve("Testing 2");
return deferred.promise;
}
console.log("ListController instantiated");
$q.all([
doTask1(),
]).then(function (value) {
alert(value);
});
});
Inside the cshtml file, (All of the angular dependencies files are in the "~/Views/Shared/_Layout.cshtml" ), there is no error in the console log hence I knew that Angular has successfully been imported in
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div ng-controller="ListController as listCtrl" class="margin-top-80px">
</div>
@section Scripts{
<script src="~/Scripts/controllers/News/ListController.js"></script>
}
This is the output. It alerts the message "testing 1"
Solution
The likely cause is that the controller is not being instantiated. For debugging purposes, include a console log to indicate instantiation of the controller:
var app = angular.module('myApp');
alert("testing 1");
app.controller('ListController', function ($scope, $q, $http,$timeout) {
function doTask1() {
var deferred = $q.defer();
deferred.resolve("testing 2");
return deferred.promise;
}
//**********DEBUGGING
console.log("ListController instantiated");
$q.all([
doTask1(),
]).then(function (value) {
alert(value);
});
});
Answered By - georgeawg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.