2016-03-02 92 views
0

我正在用angularjs做一個簡單的cms,但是我的指令有問題。無法更新angularjs指令的內容

我無法更新指令的內容。我沒有收到任何錯誤,但內容未更新。

這裏是我的代碼:

app.js

var postApp = angular.module('adminApp', [ 
    'ngRoute', 'angular-ladda' 
]); 

postApp.config(['$routeProvider', '$locationProvider', 
    function($routeProvider, $locationProvider) { 
     $routeProvider. 
     when('/posts', { 
      templateUrl: '/ngpartials/admin/blog/index.html', 
      controller: 'PostListCtrl' 
     }). 
     otherwise({ 
      redirectTo: '/posts' 
     }); 
    } 
]); 

postApp.controller('PostListCtrl', function($scope, $http){ 

    $scope.showLoading = false; 

    $scope.items = []; 

    $scope.name = 'Mike'; 
}); 

postApp.directive('helloWorld', function() { 
    return { 
     scope: { 
      name: "=" 
     }, 
     restrict: 'E', 
     template: '<h3>Hello World!!: {{ name }}</h3>' 
    }; 
}); 

模板:

<div ng-controller="PostListCtrl" class="ui grid container"> 

    <h1 class="ui header"><a href="#/posts/">Posts</a></h1> 

    <hello-world></hello-world> 

    <div ng-view></div> 

</div> 

結果:

的Hello World!:

+0

你必須具有嵌套毫無reason..please在我的答案不這樣做that..look同一控制不好的設計 –

回答

0

試試這個方法:

<hello-world name="name"></hello-world> 

指令具有隔離範圍,因此不會繼承控制器的範圍數據。您應將當前範圍的信息傳遞給指令的隔離範圍。

0

您需要的名字傳遞給指令的視圖中:

<hello-world name="name"></hello-world> 
0

月1日的問題是你的控制器兩次創建它的實例,bodyng-controller$routerProvider已加載PostListCtrl控制器的其他實例。

有了這樣的方法,創建控制器實例兩次 從設計的角度來看是沒有意義的。

,當你正在更新ng-view裝載模板是/ngpartials/admin/blog/index.html不會更新helloWorld指令name,作爲其放在外面&它得到分配給body標籤從控制器name變量的值name範圍的值,所以假設。

然後我會說,將您的指令移動到/ngpartials/admin/blog/index.html內容,例如也將名稱的值傳遞給指令元素。

<hello-world name="name"></hello-world> 
0

我更新了您的自定義指令代碼。

postApp.directive('helloWorld', function() { 

    return { 
     scope: { 
      name: "=" 
     }, 
     scope:true, 
     restrict: 'E', 
     template: '<h3>Hello World!!: {{ name }}</h3>' 
    }; 
}); 

我也在這裏安裝的jsfiddle鏈接: - http://jsfiddle.net/kpsingh/U3pVM/22839/