2014-10-08 91 views
4

我有一個局部視圖,我計劃將其包含在不同的頁面中。將部分視圖加載到主視圖中AngularJS

我嘗試了以下操作來加載部分視圖,但視圖不顯示存儲在模型中的值。

角控制器:

//This gets the required data as JSON 
AdminService.getSettings(function (callback) { 
    $scope.attributes = callback; 
    $scope.groups = _.groupBy($scope.attributes, "Group"); 
    $scope.previous = angular.copy($scope.attributes); 
    //This gets the partial view and adds to the main view 
    CommonService.SettingsListView_get(function (callback) { 
     angular.element('#settingsList').html(callback); 
    }); 
}); 

MVC控制器:

public ActionResult SettingsList() 
{ 
    return PartialView(); 
} 

查看:主

<body ng-app="AngularAdmin" ng-cloak> 
<div ng-controller="SettingsCtrl" id="top" ng-init="init()"> 
    <br /> 
    <div id="settingsList" > 

    </div> 

查看:局部

<div class="panel-group" id="accordion"> 
    <div style="padding:5px 0"><button ng-click="updateAttributes(attributes)" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Update Settings</button></div> 
    <div class="panel panel-primary" data-ng-repeat="(items, item) in groups" ng-style="$index < 11 ? panelStyle[$index] : commonPanelStyle"> 
     <div class="panel-heading" ng-style="$index < 11 ? panelHeaderStyle[$index] : commonPanelHeaderStyle"> 
      <a data-toggle="collapse" href="#accordion{{$index}}" ng-style="anchorStyle"> 
       <h4 class="panel-title"> 
        {{ items }} 
       </h4> 
      </a> 
     </div> 

    <div id="accordion{{$index}}" class="panel-collapse collapse"> 
     <div class="panel-body" ng-style="$index < 11 ? panelBodyStyle[$index] : commonPanelBodyStyle"> 
      <table class="table"> 
       <tr> 
        <th>Attribute</th> 
        <th>Description</th> 
        <th>Value</th> 
        <th>Taken from</th> 
        <th>Editable</th> 
        <th>Delete</th> 
       </tr> 
       <tr data-ng-repeat="i in item"> 
        <td> {{ i.AttributeName }} </td> 
        <td> {{ i.Description }} </td> 
        <td> <input type="text" ng-model="i.Value" class="form-control" ng-disabled="{{!i.Editable}}" /> 
        </td> 
        <td><span ng-bind="i.TakenFrom | settingsfilter">{{ Heritage }}</span> </td> 
        <td><span ng-class="i.Editable | activefilter : { icon1 : 'glyphicon-edit', icon2 : 'glyphicon-remove'}" class="glyphicon" style="font-weight: bolder"></span></td> 
        <td><span ng-click="deleteAttribute(i.AttributeGuid)" ng-class="i.TakenFrom | deletefilter : 1" class="glyphicon" style="font-weight: bolder"></span></td> 
       </tr> 
      </table> 
      <button style="float:right" class="btn btn-default" ng-click="updateAttributes(item)"><span class="glyphicon glyphicon-floppy-disk"></span> Update <em>{{ items }}</em> Settings </button> 
     </div> 
    </div> 

問題: 我無法顯示的設置數據我可以看到{{ items }},並沒有別的看法。

回答

4

實現此目的的首選方法是創建一個「settingsList」指令並將templateUrl設置爲部分視圖的url。你可以擺脫這樣的:

CommonService.SettingsListView_get(function (callback) { 
     angular.element('#settingsList').html(callback); 
    }); 

和替換這樣的:

<div id="settingsList" > 

    </div> 

與此:

<div settingsList></div> 

如果由於某種原因,這是不可能在你的情況,請嘗試更改你的控制器代碼如下(你需要注入$ compile服務):

CommonService.SettingsListView_get(function (callback) { 
     var element = angular.element(callback); 
     $compile(element)($scope); 
     angular.element('#settingsList').append(element); 
     $scope.digest(); 
    }); 
+0

控制檯中是否有任何錯誤? – Fordio 2014-10-08 10:05:26

+1

如果你在你的控制器中發現了一個斷點,$ scope.groups恰恰包含了你認爲應該做的事情?即 - 鍵是字符串,其值是對象數組的對象。 – Fordio 2014-10-08 10:22:41

0

此代碼正常工作:

CommonService.SettingsListView_get(function (callback) { 
     var element `enter code here`= angular.element(callback); 
     $compile(angular.element('#settingsList').append(element))($scope); 
}); 
相關問題