2017-05-09 56 views
0

我是AngularJS的新手不知道我在做什么错误这里是我的代码。基本上我试图从我的index.html中获得post-id的属性值,并从控制器打印到控制台中。AngularJS:从自定义标签获取属性值

在我的index.html:

<post-creator post-id="5" category="1"></post-creator> 
<script src="components/post-creator/post-creator.component.js"></script> 

后creator.component.js:

function controller($http) { 
     var model = this; 

     model.$onInit = function() { 
      console.log("id again:" + model.postId); 
     } 
module.component("postCreator", { 
     templateUrl: "components/post-creator/post-creator.template.html", 
     bindings: { 
      value: "<" 
     }, 
     controllerAs: "model", 
     controller: ["$http", controller] 
    }); 
+2

应该不是你的'bindings'包含'postId'代替'value'? – tanmay

+0

你能举个例子吗? – MTA

+0

补充说,作为答案.. – tanmay

回答

1

由于在HTML,你是路过post-id="..." category="...",他们应该是的bindings属于你component而不是value。就像这样:

bindings: { 
    postId: "<", 
    category: "<" 
} 

下面是一个示例工作示例:

var module = angular.module("myApp", []) 
 

 
function controller($http) { 
 
    var model = this; 
 

 
    model.$onInit = function() { 
 
    console.log("id again:" + model.postId); 
 
    } 
 
} 
 

 
module.component("postCreator", { 
 
    template: "<div>post creator</div>", 
 
    bindings: { 
 
    postId: "<", 
 
    category: "<" 
 
    }, 
 
    controllerAs: "model", 
 
    controller: ["$http", controller] 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <post-creator post-id="5" category="1"></post-creator> 
 
</div>