0

i am a newbie of AngularJS don't know what i am doing wrong here is my code. Basically i am trying to get attribute value of post-id from my index.html and print it in console from my controller.

In my index.html:

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

post-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]
    });
5
  • 2
    shouldn't your bindings contain postId instead of value? Commented May 9, 2017 at 6:22
  • Can you give me an example? Commented May 9, 2017 at 6:31
  • Added that as answer.. Commented May 9, 2017 at 6:33
  • Thank you for the example. I didn't notice that. Commented May 9, 2017 at 6:44
  • np, glad that helped! :) Commented May 9, 2017 at 6:44

1 Answer 1

1

Since in the HTML, you are passing post-id="..." category="...", they should be part of bindings of your component and not value. Like this:

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

Here's a sample working example:

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.