2017-09-20 24 views
0

我的控制器有問題 - 應用程序沒有進入它的內部,也沒有顯示圖像,但最糟糕的是沒有來自控制器的日誌(console.log(「AppCtrl」))。我不知道哪裏出了錯。
app.js
AngularJS未能進入控制器

(function() { 
    angular.module('AppModule', ['ui.router']) 
    .config(['$stateProvider', '$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider) { 
     $urlRouterProvider.otherwise('posts-view'); 
     $stateProvider 
     .state('posts-view', { 
      url:'/style/views', 
      templateUrl: '/posts-view.html', 
      controller: 'AppCtrl' 
     }) 
     .state('single-post-view', { 
      templateUrl: '/style/views/single-post-view.html' 
      }); 
     /*.otherwise({ 
      redirectTo: 'style/views/posts-view' 
     })*/ 
    } 
    ]); 
})(); 

controller.js

angular.module('AppModule') 
.controller('AppCtrl', AppCtrl); 
    AppCtrl.$inject = ['AppData']; 

    function AppCtrl(AppData) { 
    console.log("AppCtrl"); 
    var vm = this; //View model; 
    //vars 
    vm.posts = []; 
    //definitions 
    vm.getFoto = getFoto; 
    //inits 
    vm.getFoto(); 

    function getFoto() { 
     AppData.fetchNewPosts() 
     .then(function Success(data) { 
     vm.posts = data.data.data; 
     }, function Error(error) { 
     }); 
    } 
    } 

service.js

angular.module('AppModule') 
    .factory('AppData', AppData); 
    AppData.$inject = ['$http']; 

    function AppData($http) { 
    var exports = { 
     fetchNewPosts: fetchNewPosts, 
    }; 
    return exports; 

    function fetchNewPosts() { 
     //some not important code at the moment 
    } 
    } 

帖子,view.html

<div class="posts"> 
    <h2> Cosplay section sorted from the newest</h2> 
    <div class="row" ng-repeat="post in AppCtrl.posts"> 
     <div class="col-md-3 post-block" ng-mouseenter="option=true" ng-mouseleave="option=false"> 
     <img ng-show="{{post.images[0]}}" src="{{post.images[0].link}}" alt="image" > 
     <img ng-hide="{{post.images[0]}}" src="{{post.link}}" alt="image"> 
     <span class="title-box" ng-show="option">{{post.title}}</span> 
     </div> 
    </div> 
</div> 

應用程序進入崗位,view.html內,因爲它表明H2標籤內的文本,但僅此而已。請幫助我,我很抱歉我的英語。

回答

0

好吧,我明白了。我在index.html中加入了「post-view.html」和ng-include,這就是爲什麼我可以在post-view.html中看到標籤的原因。當我爲ma文件設置適當的路徑時,我得到了和以前一樣的結果。

但我仍然無法進入控制器變量或函數。當我試圖在控制器使用變量集:

vm.foo = "foo"; 

內部使用後view.html

<span> {{AppCtrl.foo}}</span> 

不幸的是什麼也不顯示。問題在哪裏?請幫幫我。

PS。當我使用$ scope的時候它是可以的,但我不想那樣做。我想用正確的方式使用控制器或別名的名稱來執行此操作。