2017-02-07 191 views
0

角度js不顯示任何東西,即使是簡單的表達式。我正試圖執行下面的代碼,但沒有希望。誰能幫我嗎。角度Js不能正常工作

以下代碼是用於視圖中顯示。

`<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代碼

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 = []; 
 
     } 
 
    }; 
 

 
}]);`

,請告訴我,我錯了。沒有任何工作。表達式在ouptut中顯示爲{{1 + 2}}。

+0

你有沒有在控制檯中的錯誤? – Alteyss

+0

在控制檯現在我得到的錯誤是: –

+0

的ReferenceError:的GetList不是在對象定義 。在angular.js中Object.invoke(angular.js:4708) $ get(angular.js:4547) (Object.invoke(angular.js:4708) )在Object.invoke(angular.js:4708) :4507 在d(angular.js:4654) 在E(angular.js:4678) 在Object.invoke(angular.js:4700) 在P.instance(angular.js:10177) 以n( angular.js:9096) (匿名)@ angular.js:13642 –

回答

1

您在這裏有一個錯字:

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

與2名昏迷所以依賴關係都搞砸了。

+0

呀,現在的模板是工作的罰款。但無法將服務列表返回給控制器 –

+0

請問您能否看看 –

0

我試圖用同樣的觀點不同的方式,但我修改了js文件,現在它的正常工作。

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; 
 
}]);