anand patil February 2016

AngularJS - Pass some flag from html to controller

I am new to Angular JS. Am stuck, and could not find a solution.

Problem - I have a html code as given below :

<div ng-controller="myController" ng-model={{tt="a"}}>
</div>
<div ng-controller="myController" ng-model={{tt="b"}}>
</div>
<div ng-controller="myController" ng-model={{tt="c"}}>
</div>

And a controller code is :

myApp.controller('myController', function($scope) {
    if($scope.tt == "a") //do something
    else if($scope.tt == "b) //do something else
})

NOTE - The html ng-model, I just wrote to let you know what i actually want.

Question - I want to send something to controller from my html, so that i can check the value and do conditionally.

Answers


James Choi February 2016

This is probably not the best practice but if you want to bring data in from the html to angular you can stash the data as a property of window inside of a <script> tag.

var app = angular.module('myApp',[]);
app.controller('mainCtrl', function($scope){
  $scope.tt = window.data;
})
<div ng-app='myApp'>
  <div ng-controller='mainCtrl'>
    {{tt}}
  </div>
</div>




<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>

<script>
  window.data='abcd';
</script>

Post Status

Asked in February 2016
Viewed 3,594 times
Voted 13
Answered 1 times

Search




Leave an answer