2016-03-02 93 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/