AngularJS:从自定义标记获取属性值

时间:2017-05-09 06:19:08

标签: javascript angularjs

我是AngularJS的新手,不知道我做错了什么,这是我的代码。基本上我试图从我的index.html获取post-id的属性值,并从我的控制器在控制台中打印它。

在我的index.html中:

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]
    });

-creator.component.js后:

   <input name="comments" ng-model="comments" class="form-control" type="text">

1 个答案:

答案 0 :(得分:1)

由于在HTML中,您传递的是post-id="..." category="...",因此它们应该属于bindings的{​​{1}}而不是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]
});