UPDATE: Apparently setting the controller in the view such ng-controller="SomethingController as Ctrl"
and then using it in the model ng-model="Ctrl.myModel"
works. What the f***?
I've been meaning to implement this specific directive on my application. It simply requires me to put my model as an attribute of the element as ng-model="myModel"
and it should work. I see examples of it working, and I've made it work on jsfiddle too.
However it simple does not want to work on my app. I have installed through bower, I've included all the files in my index.html
and I've also injected it angular.module('myApp', ['angular-input-stars'])
.
Now the issues is, the model only works one way through on my application. The ng-model="myModel"
uses myModel
's value and sets the correct amount of stars, but once I change the stars (in and through the directive) that value is not being sent back to my model.
I've checked the directive's code and they do use the model controller set the value:
function link(scope, element, attrs, ngModelCtrl) {
//...
scope.setValue = function (index, e) {
//...
ngModelCtrl.$setViewValue(scope.last_value);
}
}
And even though the directive does not include in the return
that scope: {ngModel: '='}
, this still works on examples and jsFiddle, even though I don't really understand the $setViewValue
(even after researching about it).
I think it may have something to do with my application's architecture, but I'm not sure. The app is very robust and well structured, and I cannot display it's code (company code), but I can say how we structured it:
|-- app.js (contains directive module, show bellow)
|-- index.html
|-- /modules
|-- /something
|-- /overview
|-- somethingController.js
(included in index.html & loaded through directive)
|-- somethingDirective.js
(included in index.html & loaded through app.js module)
|-- somethingView.html
(loaded through directive)
app.js code:
var app = angular.module('myApp', [
'angular-input-stars',
'something.overview'
]);
somethingDirective.js:
var somethingOverview = angular.module('something.overview');
somethingOverview.directive('somethingOverviewDirective', function () {
return {
restrict: 'E',
controller: 'SomethingOverviewController',
templateUrl: 'modules/something/overview/somethingOverviewView.html',
scope: {
overview: '=overview',
errors: '=errors'
}
};
});
somethingController.js:
var somethingOverview = angular.module('something.overview', []);
somethingOverview.controller('SomethingOverviewController',
['$scope', function ($scope) {
function init() {
$scope.myModel = 5;
}
// ...
init();
}]);
somethingView.html:
<div>
Rating: {{myModel}}
<input-stars max="5" ng-model="myModel"></input-stars>
</div>