简体   繁体   中英

Angular Js is not working properly

angular js not displaying anything even like simple expressions. i am tying to execute below code but no hope. can anyone help me out.

below code is for view to display.

 `<html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <script type="text/javascript" src="/../../Scripts/angularsample.js"></script> </head> <body ng-app="spiceApp"> <div> <div ng-controller="SpicyController"> <p> lets try some code by using service </p> <input ng-init="message='Girish'" ng-model="message" /> <button ng-click="notify(message);">Notify{{1+2}}</button> <p>alert will display only by clicking three times.</p> </div> <div ng-controller="List"> <button ng-click="bringList()">getList</button> <table> <tr ng-repeat="app in appslist"> <td> {{app.Name}} </td> </tr> </table> </div> </div> </body> </html>` 

js code

 var myApp = angular.module('spiceApp', []); myApp.controller('SpicyController', ['$scope', '$http', 'userService', , function ($scope, $http, userService) { //below code is using sservice $scope.notify = function (msg) { userService(msg); }; }]); myApp.controller('List', ['$scope', 'getList', function ($scope, getList) { $scope.bringList = function () { getList.getAppsList().then(function (list) { $scope.appslist = list; }); }; }]); myApp.factory('getList', ['$http',function ($http) { //this code for getting list from controller. return getList.getAppsList=function(){ $http({ method: 'GET', url: 'Home/GetAppsList' }) .success(function (response) { return response.data; }, function (error) { console.log(error); }); } }]); myApp.factory('userService', ['$window', function (win) { var msgs = []; return function (msg) { msgs.push(msg); if (msgs.length == 3) { win.alert(msgs.join('\\n')); msgs = []; } }; }]);` 

please tell me where i am wrong. nothing is working. expression is displaying like {{1+2}} in the ouptut.

You have a typo here:

myApp.controller('SpicyController', ['$scope', '$http', 'userService', , function 

with the 2 comas so the dependancies are messed up.

i tried in different way with same view but i modified the js file now it's working fine.

 var myApp = angular.module('spiceApp', []); myApp.controller('SpicyController', ['$scope', '$http', 'userService',function ($scope, $http, userService) { //below code is using sservice $scope.notify = function (msg) { userService(msg); }; }]); myApp.factory('userService', ['$window', function (win) { var msgs = []; return function (msg) { msgs.push(msg); if (msgs.length == 3) { win.alert(msgs.join('\\n')); msgs = []; } }; }]); myApp.controller('List', ['$scope', 'getList', function ($scope, getList) { $scope.bringList = function () { getList.getAppsList.then(function (data) { $scope.appslist = data; }); }; }]); var getList = angular.module("spiceApp").factory("getList", ['$http', function ($http, getList) { return { getAppsList: (function (response) { return $http({ method: 'GET', url: 'GetAppsList' }) .then(function (response) { console.log("coming from servicejs", response.data); //return data when promise resolved //that would help you to continue promise chain. return response.data; }); })() }; return getList; }]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM