2016-02-29 30 views
0

我試圖渲染部分視圖上的點擊,其中有控制器。ng-click(s)在部分視圖中不起作用

佈局/索引頁:

<!DOCTYPE html> 
<html lang="en" ng-app="AngularDemo"> 
    <head> 
     .. 
    </head> 
    <body> 
     <aside> 
      <ul class="nav nav-list" ng-controller="Navigations as nav"> 
       <li class=""><a href="#" data-thisdiv="HomeDiv" ng-click="nav.showHomeDiv($event)">Home</a></li> 
       <li class=""><a href="#" data-thisdiv="FedbackDiv" ng-click="nav.showFeedbackDiv($event)"> Feedbacks</a></li> 
      </ul> 
     </aside> 
     <section id="MainBody"></section> 
    </body> 
</html> 

controller.js

myApp.controller("Navigations", ["$compile", function ($compile) { 
    var $this = this; 
    this.showFeedbackDiv = function ($event) { 
     var compiledeHTML = $compile("<div load-Div></div>")($this); 
     $("#MainBody").append(compiledeHTML); 
     $(".Division").hide(); 
     $("." + $(event.currentTarget).data("thisdiv")).show(); 

    }; 
}]).directive('loadDiv', function() { 
    return { 
     templateUrl: '/Default/GetFeedbackView', 
     controller: 'Feedback' 
    }; 
}); 

myApp.controller("Feedback", ['AngService', '$element', function(AngService, $element) { 
    this.feedbackData = {}; 
    var $this = this; 
    this.ShowFeedbacks = function() { 
     AngService.ShowFeedbacks().then(function (response) { 
      $this.allFeedbacks = JSON.parse(response.data); 
      console.log($this.allFeedbacks); 
      $("#ShowFeedbacksModal").modal({ backdrop: 'static', keyboard: false, show: true }); 
     }, function (response) { 
      alert('Error in getting feedbacks'); 
     }); 
    }; 
}]); 

局部視圖:

<div class="Division FedbackDiv" style="margin-top:40px" ng-controller="Feedback as fb"> 
    <div class="page-header"> 
     <h1> 
      Feedback form 
      <span style="cursor:pointer;" class="pull-right" ng-click="fb.ShowFeedbacks()"><i class="fa fa-adn"></i></span> 
     </h1> 
    </div> 
    <div class="row"> 
     ... 
    </div> 
</div> 

,當我在 「秀反饋」 點擊沒有反應 錯誤:

TypeError: a.$new is not a function
at g (angular.js:6970)
at M (angular.js:7618)
at angular.js:7854
at angular.js:12914
at h.$eval (angular.js:14123)
at h.$digest (angular.js:13939)
at h.$apply (angular.js:14227)
at p (angular.js:9493)
at J (angular.js:9678)
at XMLHttpRequest.t.onload (angular.js:9621)(anonymous function) @ angular.js:11358

回答

1

應該通過在$ $編譯範圍; $範圍應該注入控制器;如果檢查對象這一點,它不是範圍

,而不是

var compiledeHTML = $compile("<div load-Div></div>")($this); 

嘗試

var compiledeHTML = $compile("<div load-Div></div>")($scope); 

要注入$範圍

myApp.controller('Navigations', ['$compile', '$scope', function ($compile, $scope) { ... }]) 
+0

錯誤:$範圍不是定義爲 – Arjun

+0

正如我前面提到的,您將不得不向控制器注入$ scope *讓我編輯我的原始發佈 – truthseeker

1

刪除NG-控制器=「反饋作爲從部分FB和添加controllerAs:FB到loadDiv配置

.directive('loadDiv', function() { 
    return { 
    templateUrl: '/Default/GetFeedbackView', 
    controller: 'Feedback', 
    controllerAs : 'fb' 
    }; 
+0

我刪除NG控制器=「反饋fb「從部分視圖和添加控制器as:fb加載區域 .directive('loadDiv',function (){ return template_url:'/ Default/GetFeedbackView', controller:'Feedback', controllerAs:'fb' }; eror:ReferenceError:fb未定義 – Arjun

+0

請致電反饋控制器嗎? –

+0

可能會幫助你http://blog.richpollock.com/2014/10/angular-js-tabs-directive-with-dynamic-loading-of-partial-templates-and-controllers/ –

相關問題